Include and Forwarding request in JSP


Static include

This is a process to include the content of a file into a jsp. The include file can be a jsp, html or txt file. This inclusion happens during the conversion of jsp into the servlet. This is required to avoid duplicate code in multiple jsp. If a common task is required to be performed by a set of jsp files then that code can be written in a file and that file can be included in the required jsp. This can be done by using name of the include file into the file attribute of include directive. This directive can be used in any place and any number of times in a jsp.

Example:

<html>
<head>
</head>
<body>
<h1 align="center">From jsp</h1>
<%@ include file="./x.jsp"%>
<h1 align="center">From html</h1>
<%@ include file="./y.html"%>
<h1 align="center">From text</h1>
<%@ include file="./z.txt"%>
</body>
</html>

Forwarding request


Using <jsp:forward>

This tag transfers request available to a jsp to another page or a jsp. The second page can use the request available to the first jsp. It also transfers the execution control to the second page. The execution control never comes back to the first jsp. Hence, the code present after this tag cannot execute.

Attribute

Page:- This contains name and path of forward page.
<jsp:forward page="./x.jsp"/>

Using <jsp:include>

This tag transfers request and execution control to another page or jsp to include response of that page. The execution control comes back to the first jsp. The first jsp provides response to the user by including response of second page. A jsp can include response of any number of pages. The user gets response from multiple pages as a single output.

Attribute

Page:- This contains name and path of the include page.

<jsp:include page="./x.jsp"/>

Using <jsp:param>

This tag creates a parameter in the request.This tag must be used as the sub tag of <jsp:forward> or <jsp:include> tag.

It can be used to create multiple parameters in the request.When the request gets transferred then the forward page or include page can use the parameter created by this tag through getParameter() of the request.

Attribute

Name:- It contains name of the parameter to be created.
Value:- It contains value of the parameter.

Difference between jsp include and static include

JSP include

  1. This includes response of the page into the jsp.
  2. This is called as request time inclusion.
  3. It can include only the response of a web page.
  4. The web server generates different servlets for each include page.

Static include

  1. This includes code of a file into the jsp.
  2. This is called as translation time inclusion.
  3. This can include code of any type of file.
  4. The web server generates only one servlets for all included file in current jsp.

Example:- To show how to use of forwarding request in a application. The scenario is when the user provides his/her correct credentials (accountno, pinno) into the textbox and click a submit button he/she is redirected to the balance page showing the balance present in his/her account.

Creation of a context file

Create any folder in any drive as (E:\Jsp). Inside that folder store your .jsp files. Give the Context path name as jsp and docBase as E:\Jsp, here docBase means the total path where we are storing our .jsp files. These changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Table creation

First create an account table in Oracle and insert some data as below

create table account(accountno varchar(50),pinno int,balance int)

insert into account values('Raj',123,1000)
insert into account values('Ravi',456,2000)
insert into account values('Rahul',789,3000)

Creation of dsn (database source name)

Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.
Note:- Here Username=system, Password=pintu and Dsn name=dsn2

Account.html

<html>
<head>
<script language="javascript">
function validate(objForm){
if(objForm.t1.value.length==0){
alert("Please enter Account No!");
objForm.t1.focus();
return false;
}
if(objForm.p1.value.length==0){
alert("Please enter Pin No!");
objForm.p1.focus();
return false;
}
return true;
}
</script>
</head>
<body >
<form name ="objForm" action="./validate.jsp" method="post" onSubmit="return validate(this)">
<table>
<tr>
<td><b>Account No.</b></td>
<td><input name="t1" type="text " /></td>
</tr>
<tr>
<td><b>Pin No.</b></td>
<td><input name="p1" type="password" /></td>
</tr>
</table>
<input type="submit" value="submit"/>
</form>
</body>
</html>

validate.jsp

<%@ page import="java.sql.*" %>
<%
String str1=request.getParameter("t1");
String str2=request.getParameter("p1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn2","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from account where accountno='"+ str1 +"' and pinno="+str2 );
if(rs.next())
{
int b=rs.getInt("balance");
%>
<jsp:forward page="/balance.jsp">
<jsp:param name="pr" value="<%= b %>" />
</jsp:forward>
<%
}
else
{

%>
<html>
<body>
<h1>Invalid Account No/Pin No </h1>
<jsp:include page="Account.html" />
<%
}
%>
</body>
</html>

balance.jsp

<html>
<body>
<%
String str1=request.getParameter("t1");
String str2=request.getParameter("pr");
%>
<h1>Account balance of <font color='#663300'><%= str1 %></font> is <font color='red'><%= str2 %></font> </h1>
</body>
</html>

Running the application

Run the tomcat then write the below line in the URL

http://localhost:8081/jsp/

Here jsp is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

After giving the URL a set a listing will come, here only three appears As Account.html, balance.jsp, validate.jsp. Click Account.html and provide the correct credentials to know the balance.

JSP.gif

Up Next
    Ebook Download
    View all
    Learn
    View all