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>.
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.
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>
<p class="text-str"><strong>Syntax:</strong></p>
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/>?
Answer:
<web-app> </web-app>
<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>
<% 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"/> <% }%>
<h5>Login Successful</h5> Welcome to : <h4><%= request.getParameter("login_uname")%></h4>
Develop a form?
Answer:
Student table:
create table student ( stno number (3), stname varchar2 (15), college varchar2 (20), marks number (5,2), dob date ); /
<web-app> </web-app>
<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"> <input type="reset" value="Clear"> </form> </center> </body> </html>
<%@ 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>
<%@ 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"> <input type="reset" value="Clear"> </form> </center> </body> </html>
<%@ 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"> <input type="reset" value="Clear"> </form> </center> </body> </html>
<%@ 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>