This is a process to include 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 required to be performed by a set of jsp 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.
<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"%>
This tag transfer 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.
Page:- This contains name and path of forward page.
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.
Page:- This contains name and path of the include page.
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.
Name:- It contains name of the parameter to be created.
Value:- It contains value of the parameter.
Difference between jsp include and static 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.
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.
Ex:- 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 submit button he/she is redirect 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.
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
alert("Please enter Account No!");
alert("Please enter Pin No!");
<form name ="objForm" action="./validate.jsp" method="post" onSubmit="return validate(this)">
<td><input name="t1" type="text " /></td>
<td><input name="p1" type="password" /></td>
<input type="submit" value="submit"/>
<%@ page import="java.sql.*" %>
ResultSet rs=stmt.executeQuery("select * from account where accountno='"+ str1 +"' and pinno="+str2 );
<jsp:param name="pr" value="<%= b %>" />
<h1>Invalid Account No/Pin No </h1>
<jsp:include page="Account.html" />
<h1>Account balance of <font color='#663300'><%= str1 %></font> is <font color='red'><%= str2 %></font> </h1>
Running the application
Run the tomcat then write the below line in the Url
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.