Book of tasks on programming. Old version

 

 by Aliaksandr N. Prykhodzka

 

online, ñàìîîáó÷åíèå, row, CD-ROM, examples, îáó÷åíèå, or, java, object, ðåàëèçàöèÿ, format, cycle, ïîëèìîðôèçì, òðàíñëÿòîð, ðÿä, ïðîëîã, interruption, ðåàëèçàöèÿ
 

for valuable work you must have JavaScript (allow active content)

JSP. D.4. Nest of tasks. JSP. JSTL:Core. Tags (c:set), (c:if), (c:out)

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.1     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k1" value="${3}"/>
<c:set var="k2" value="${5}"/>
<c:set var="k3" value="${7}"/>
<c:if test="${k1>1}" >
    <c:if test="${k2<k3}" >
        <c:set var="dd" value="4"/>
    </c:if>
    <c:if test="${!(k2<k3)}" >
        <c:set var="dd" value="3"/>
    </c:if>
</c:if>
<c:if test="${!(k1>1)}" >
    <c:set var="dd" value="2"/>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.2     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k1" value="${3}"/>
<c:set var="k2" value="${5}"/>
<c:set var="k3" value="${7}"/>
<c:if test="${k1>10}">
    <c:set var="dd" value="1"/>
</c:if>
<c:if test="${!(k1>10)}">
    <c:if test="${k1 % 3==1}">
        <c:set var="dd" value="2"/>
    </c:if>
    <c:if test="${!(k1 % 3==1)}">
        <c:if test="${k1 % 5==1}">
            <c:set var="dd" value="3"/>
        </c:if>
        <c:if test="${!(k1 % 5==1)}">
            <c:if test="${k1 % 3==2}">
                <c:set var="dd" value="4"/>
            </c:if>
            <c:if test="${!(k1 % 3==2)}">
                <c:set var="dd" value="5"/>
            </c:if>
        </c:if>        
    </c:if>
</c:if>   
<c:if test="${!(k1>1)}" >
    <c:set var="dd" value="2"/>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.3     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k1" value="${6}"/>
<c:set var="k2" value="${10}"/>
<c:set var="k3" value="${21}"/>
<c:if test="${k1>k2}">
    <c:set var="dd" value="1"/>
</c:if>
<c:if test="${!(k1>k2)}">
    <c:if test="${k1>k3}">
        <c:set var="dd" value="2"/>
    </c:if>
    <c:if test="${!(k1>k3)}">
        <c:if test="${k1+k2>k3}">
            <c:set var="dd" value="3"/>
        </c:if>
        <c:if test="${!(k1+k2>k3)}">
            <c:set var="dd" value="4"/>            
        </c:if>        
    </c:if>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.4     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k" value="${40}"/>
<c:if test="${k % 3==0}">
    <c:set var="dd" value="1"/>
</c:if>
<c:if test="${!(k % 3==0)}">
    <c:if test="${k % 3==1}">
        <c:set var="dd" value="2"/>
    </c:if>
    <c:if test="${!(k % 3==1)}">
        <c:if test="${k % 3==2}">
            <c:set var="dd" value="3"/>
        </c:if>
        <c:if test="${!(k % 3==2)}">
            <c:set var="dd" value="4"/>            
        </c:if>        
    </c:if>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.5     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k" value="${45}"/>
<c:if test="${k / 3==13}">
    <c:set var="dd" value="1"/>
</c:if>
<c:if test="${!(k / 3==13)}">
    <c:if test="${k / 3==14}">
        <c:set var="dd" value="2"/>
    </c:if>
    <c:if test="${!(k / 3==14)}">
        <c:if test="${k / 3==15}">
            <c:set var="dd" value="3"/>
        </c:if>
        <c:if test="${!(k / 3==15)}">
            <c:if test="${k / 3==16}">
                <c:set var="dd" value="4"/>
            </c:if>
            <c:if test="${!(k / 3==16)}">
                <c:set var="dd" value="5"/>            
            </c:if>                
        </c:if>        
    </c:if>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.6     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


<c:set var="k" value="${44}"/>
<c:set var="dd" value="0"/>
<c:if test="${k % 2==0}">
    <c:set var="dd" value="${dd+1}"/>
</c:if>
<c:if test="${k % 3==1}">
    <c:set var="dd" value="${dd+2}"/>
</c:if>
<c:if test="${k % 5==4}">
    <c:set var="dd" value="${dd+5}"/>
</c:if>
<c:if test="${k % 6==4}">
    <c:set var="dd" value="${dd+9}"/>
</c:if>
<c:out value="${dd}"/>

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.4.7     Answer

We have a code's fragment of JSP-page. Determine a sequence of numbers produced by this code


Òåêñò ôàéëà jsp1.jsp

<%@ page contentType="text/html; charset=windows-1251" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<jsp:useBean id="bean0" scope="session" class="pr_c.Enterprise1Bean" />
<jsp:setProperty name="bean0" property="*" />
<body bgcolor="#ffffff">
<form method="post" action="jsp1.jsp">
<hr><br>
Ââåäèòå çíà÷åíèå: <input name="pp"><br>
Ââåäèòå èìÿ ïîëüçîâàòåëÿ: <input name="sessionContext"><br>
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


Òåêñò ôàéëà Interprise1Bean.java

package pr_c;

import java.rmi.*;
import javax.ejb.*;
import javax.naming.NamingException;
import java.sql.SQLException;
import javax.transaction.SystemException;
import javax.transaction.NotSupportedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.RollbackException;

public class Enterprise1Bean implements SessionBean {
SessionContext sessionContext;
String pp;

public void ejbCreate() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}

public void doInsert(String Par1) throws NamingException, SQLException,
SystemException, NotSupportedException, IllegalStateException,
SecurityException, HeuristicRollbackException,
HeuristicMixedException, RollbackException {

javax.transaction.UserTransaction utx;
javax.sql.DataSource dataSour;
java.sql.Connection Conn;
java.sql.Statement Stmt;

javax.naming.Context context = new javax.naming.InitialContext();

dataSour = (javax.sql.DataSource)
context.lookup("java:comp/env/jdbcDatabase1");
Conn = dataSour.getConnection();

Stmt = Conn.createStatement();

utx = sessionContext.getUserTransaction();

utx.begin();

Stmt.executeUpdate("insert into Tabl1 (XX) values ("+Par1+")");

utx.commit();

Stmt.close();
Conn.close();
}

public String getPp() {
return pp;
}

public void setPp(String pp) {
this.pp = pp;
try {
doInsert(pp);
}
catch (RollbackException ex) { }
catch (HeuristicMixedException ex) { }
catch (HeuristicRollbackException ex) { }
catch (SecurityException ex) { }
catch (IllegalStateException ex) { }
catch (NotSupportedException ex) { }
catch (SystemException ex) { }
catch (SQLException ex) { }
catch (NamingException ex) {
}
}

}

 

©   Aliaksandr Prykhodzka    1993 - 2007