Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Include and Forward request in JSP

In this article, we will learn about Static include process and the way to forward requests making use of JSP.

Introduction to Static include

Static include is a process so that the content of a file is included into a jsp and this includes file can be a jsp, html or txt file. When this inclusion does happen? This actually takes place at the time of converting of jsp into the servlet which is basically done to avoid duplicate code in multiple jsp. In case a common task is needed to be performed by a set of jsp files, in that situation we can have a code that can be written in a file. Also this file can be included in the required jsp. This can be done by making use of a name of the include file into the file attribute of include directive that can be utilized in any place and any number of times in a jsp.

Listing1: Example displaying the usage of a include file

<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>

How Request can be forwarded?

Using <jsp:forward>

The tag shown above is used to transfer request that is available to a jsp to another page or a jsp. The second page can make use of the available request to the first jsp and also transfers the execution control to the second page. And more importantly you will never see the execution control coming back to the first jsp thereby making it impossible for the code present after this tag to be executed.

Introduction to Attribute

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

The tag displayed above is used to transfer request and execution control to another page or jsp so as to comprise response of that page and we have execution control coming back to the first jsp. The first jsp is used to provide response to the user by including response of second page and we can have a jsp that can comprise of a response of any number of pages. The user receives response from multiple pages as a single output.

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

Using <jsp:param>

The above tag creates a parameter in the request and must be used as the sub tag of <jsp:forward> or <jsp:include> tag. Also it can be made use so as to create multiple parameters in the request. The moment the request gets transferred, we have a forward page or include page that can use the parameter created by this tag via getParameter() of the request.

Attribute

  • Name: - It comprises of name of the parameter that needs to be created.
  • Value: - It comprises of value of the parameter.

What is the difference between jsp include and static include?

  • JSP Include gives Response of the page into the jsp whereas Static Include code of a file into the jsp.
  • JSP include is called as request time inclusion whereas Static include is called as translation time inclusion.
  • JSP include can include only the response of a web page whereas Static include code of any type of file.
  • In JSP Include, the web server generates different servlets for each include page whereas in Static Include, the web server that generates only one servlets for all included file in current jsp.

Context files creation

In order to create context file, create any folder in any drive as (E:\Jsp) and store your .jsp files in that folder. You can give the Context path name as jsp and docBase as E:\Jsp. By docBase, we mean the total path where the .jsp files are stored and the changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Creation of a Table

You need to create an account table in Oracle and incorporate some data as shown 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)

Database source name creation

The steps to create database source name are:

  • Start> Control panel> Administrative Tools> Data Sources (ODBC)> go to system dsn tab
  • Click add button>Select a driver for which the data source need to be set up (for Oracle- Oracle in XE)> select it and then access finish
  • Give any name in data source name textbox> click ok button.

Listing 2: Representing the 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>

Listing 3: Representing the 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>

Listing 4: Representing the 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>

Executing or Running the application

Post giving the URL, a set a listing will come, here only three appears As Account.html, balance.jsp, validate.jsp. Click Account.html and then provide the correct credentials so that the balance can be known.

Outcome of the Execution of an Application

Figure 1: Outcome of the Execution of an Application

Conclusion

We learned the process of a Static include and how the request can be forwarded making use of JSP.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login