Java Server Pages Users Passing Control and Data between Pages

Sometimes, we need to hand over the control to another page, with the necessary data passed to it. In this section, we shall discuss how to pass control across pages with data passed to them.

1. Passing Control

Sometimes, a JSP page wants to pass the control to another server-side program for further processing. For example, in an e-mail application, the login.jsp page on user verification may want to forward the request to the home.jsp page. This is done using the <jsp:forward> action in the login.jsp page as follows:

String user = request.getParameter(”user”);

String password = request.getParameter(”password”);

//verify the user here

<jsp:forward page=”home.jsp” />

Once the JSP engine encounters the <jsp:forward> action, it stops the processing of the current page and starts the processing of the page (called target page) specified by the page attribute. The rest of the original page is never processed further.

The HttpServietRequest and HttpServietResponse objects are passed to the target page. So, the target page can access all information passed to the original page. You can pass additional parameters to the target page using the <jsp:param> action as follows.

String user = request.getParameter(”user”);

String password = request.getParameter(”password”);

//verify the user here

double startTime = System.currentTimeMillis();

<jsp:forward page=”home.jsp”>

<jsp:param name=”startTime” value=”<%=startTime%>” />


The target page can access these parameters in the same way as the original parameters, using the getParameter() and/or getParameterNames() methods.

2. Passing Data

The scope of an object indicates from where the object is visible. JSP specification defines four types of scopes: page, request, session, and application. The objects having page scope are only available within the page. So, objects that are created with page scope in a page are not available in another page where the control is transferred using the <jsp:forward> action. If you make an object visible across multiple pages across the same request, create the object with request scope. The following example illustrates this.


<jsp:useBean id=”fact” scope=”request” class=”bean.Factorial” />


<jsp:forward page=”new.jsp” />

This page creates a bean object with the request scope and sets the value property with 5. It then passes the control to the new.jsp page, which looks like this.


<jsp:useBean id=”fact” scope=”request” class=”bean.Factorial” />


Since the bean was saved as the request scope in the original.jsp page, the <jsp:useBean> action in the new.jsp page finds and uses it.

For the URL http://i27.0.0.i:8080/net/originai.jsp, the following is displayed:


Depending upon your requirement, you may create an object having any one of the four scopes.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *