JSP Tutorial

Standard Actions

Standard Actions:

These are basically used to pass runtime information to the container. As a part of JSP we have the following standard actions; they are <JSP:forward/>, <JSP:include/>, <JSP:param/>, <JSP:useBean/>, <JSP:setProperty/> and <JSP:getProperty>.

  1. <JSP:forward/>:

    When we want to forward a request and response to the destination JSP page from the source JSP we must use <JSP:forward>.

    Syntax:

    Without body:

    <JSP:forward page="relative or absolute path of JSP page"/>
                                    

    With body:

    <JSP:forward page=" relative or absolute path of JSP page">
        <JSP:param name="param name1" value="param value1"/>
        <JSP:param name="param name2" value="param value2"/>
    </JSP:forward>
                                    

    For example:

    <JSP:forward page="y.jsp">
        <JSP:param name="v1" value="10"/>
        <JSP:param name="v2" value="20"/>
    </JSP:forward>
                                    
    When we use this tag we get the response of destination JSP page only but not source JSP page.
  2. <JSP:include/>:

    This tag is used for processing a client request by a source JSP page by including other JSP pages and static resources like html's. One source JSP can include 'n' number of server side resources and finally we get the response of source JSP only.

    Syntax:

    Without body:

    <JSP:include page="relative or absolute path of JSP page"/>
    

    With body:

    <JSP:include page=" relative or absolute path of JSP page">
        <JSP:param name="param name1" value="param value1"/>
        <JSP:param name="param name2" value="param value2"/>
    </JSP:include>
    

    For example-1:

    <JSP:include page="y.jsp">
        <JSP:param name="v1" value="10"/>
        <JSP:param name="v2" value="20"/>
    </JSP:include>
    

    For example-2:

    <JSP:include page="z.jsp">
        <JSP:param name="v3" value="30"/>
        <JSP:param name="v4" value="40"/>
    </JSP:include>
    
  3. <JSP:param/>:

    This tag is used for passing the local data of one JSP page to another JSP page in the form of (key, value) pair.
    <p class="text-str"><strong>Syntax:</strong></p>
    
    Here, name represents name of the parameter or attribute and it must be unique value represents value of the parameter and should be always string. <JSP:param/> tag should be used in connection with either <JSP:forward/> or <JSP:include/>.

    For example-1:

    <JSP:forward page="y.jsp">
        <JSP:param name="v1" value="10"/>
        <JSP:param name="v2" value="20"/>
    </JSP:forward>
    

    For example-2:

    <JSP:include page="y.jsp">
        <JSP:param name="v1" value="10"/>
        <JSP:param name="v2" value="20"/>
    </JSP:include>
    

    For example-3:

    <JSP:include page="z.jsp">
        <JSP:param name="v3" value="30"/>
        <JSP:param name="v4" value="40"/>
    </JSP:include>
    

Write a JSP page which illustrates the concept of <JSP:forward/> and <JSP:include/>?

illustrates concept

Answer:

web.xml:

<web-app>
</web-app>

Login.html:

<html>
    <head><center><h3>Login Page</h3></center></head>
    <body>
    <center>
        <h4>Forward/Include test</h4>
        <form name="login" action="Login.jsp" method="post">
            <p>Enter username : <input type="text" name="login_uname" value=""><br>
                Enter password : <input type="password" name="login_pwd" value=""><br>
                <input type="submit" value="Login">
        </form>
    </center>
    </body>
</html>

Login.jsp:

<%	String s1 = request.getParameter("login_uname");
        String s2 = request.getParameter("login_pwd");
        if (s1.equals("kalpana") && s2.equals("test")) {%>
        <JSP:forward page="Success.jsp"/>
<%	} else {%>
        <h5>Login failed</h5>
        <JSP:include page="Login.html"/>
<%	}%>

Success.jsp:

<h5>Login Successful</h5> Welcome to :&nbsp;
<h4><%= request.getParameter("login_uname")%></h4>

Develop a form?

Develop the form

Answer:

Student table:

create table student (
stno number (3),
stname varchar2 (15),
college varchar2 (20),
marks number (5,2), dob date
);
/

web.xml:

<web-app>
</web-app>

login.html:

<html>
    <body>
    <center>
        <form name="login" action="security.jsp" method="post">
            <table>
                <tr>
                    <th align="left">Enter username : </th>
                    <td><input type="text" name="login_uname" value=""></td>
                </tr>
                <tr>
                    <th align="left">Enter password : </th>
                    <td><input type="password" name="login_pwd" value=""></td>
                </tr>
            </table>
            <input type="submit" value="Login">&nbsp;&nbsp;&nbsp;
            <input type="reset" value="Clear">
        </form>
    </center>
    </body>
</html>

security.jsp:

<%@	page session="true" %>
<html>
<%	String s1 = request.getParameter("login_uname");
        String s2 = request.getParameter("login_pwd");
        if (s1.equals("student") && s2.equals("test"))
        {%>
        <JSP:forward page="first.jsp"/>
<%	} else 
        {%>
        <h4>Login Failed</h4>
        <JSP:include page="login.html"/>
<%	}%>
</html>

first.jsp:

<%@	page session="true"	%>
<html>
    <body>
    <center>
        <form name="first" action="second.jsp" method="post">
            <table>
                <tr>
                    <th align="left">Enter number : </th>
                    <td><input type="text" name="first_stno" value=""></td>
                </tr>
                <tr>
                    <th align="left">Enter name : </th>
                    <td><input type="text" name="first_stname" value=""></td>
                </tr>
            </table>
            <input type="submit" value="Send">&nbsp;&nbsp;&nbsp;
            <input type="reset" value="Clear">
        </form>
    </center>
    </body>
</html>

second.jsp:

<%@	page session="true"	%>
<html>
    <body>
    <center>
        <form name="second" action="third.jsp" method="post">
            <table>
                <tr>
                    <th align="left">Enter college name : </th>
                    <td><input type="text" name="second_cname" value=""></td>
                </tr>
                <tr>
                    <th align="left">Enter marks : </th>
                    <td><input type="text" name="second_marks" value=""></td>
                </tr>
                <tr>
                    <th align="left">Enter date of birth : </th>
                    <td><input type="text" name="second_dob" value=""></td>
                </tr>
            </table>
            <input type="submit" value="Send">&nbsp;&nbsp;&nbsp;
            <input type="reset" value="Clear">
        </form>
    </center>
    </body>
</html>

third.jsp:

<%@ page session="true" %>
<%@ page import="java.sql.*, java.io.*"	%>
<html>
    <%!	Connection con = null;
        PreparedStatement ps = null;
        public void jspInit() {
            try 
            {   Class.forName("oracle.jdbc.driver.OracleDriver");
                con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Hanuman", "scott", "tiger");
                ps = con.prepareStatement("insert into student values (?, ?,?, ?,?)");
            }
             catch (Exception e) 
             {
                out.println(e);
            }
        }%>
    <%	String stno = request.getParameter("first_stno");
        String name = request.getParameter("first_stname");
        String college = request.getParameter("second_cname");
        String marks1 = request.getParameter("second_marks");
        String dob1 = request.getParameter("second_dob");
        int number = Integer.parseInt(stno);
        float marks = Float.parseFloat(marks1);
        Date dob = Date.parseDate(dob1);
    %>
    <%!	try
            {
                ps.setInt(1, number);
                ps.setString(2, name);
                ps.setString(3, college);
                ps.setFloat(4, marks);
                ps.setDate(5, dob);
                int i = ps.executeUpdate();
                if (i > 0) {%>
                <html>
                    <body bgcolor="lightblue">
                    <center>
                        <h3>Inserted Successfully</h3>
                    </center>
                    <a href="login.html">One more</a>
                    </body>
                </html>
    <%	}
                         else 
                     { %>
    <html>
        <body bgcolor="lightblue">
        <center>
            <h3>Try again</h3>
        </center>
        <a href="login.html">Home</a>
        </body>
    </html>
    <%	}
        }
        catch (exception e)
    {
    out.println(e);
        }%>
</html>