Passing Values between JSP Pages

This section provides a tutorial example on how to use different ways to pass values between JSP pages: putting values in 'session' or 'application' objects, or putting values at the end of redirect URL.

There are many ways to pass values from one page to the next page. For example,

In the following example, I am showing you a tutorial example on how to use different ways to pass values between JSP pages. In this example, I have two JSP pages working together as a registration process. Here is the first JSP page, RegForm.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- RegForm.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<jsp:declaration><![CDATA[
   private String getItem(String queryString, String key) {
      String value = null;
      if (queryString!=null) {
         int i = queryString.indexOf(key);
         if (i>-1) {
            i = i + key.length();
            int j = queryString.indexOf("&",i);
            if (j>-1) {
               value = queryString.substring(i,j);
            } else {
               value = queryString.substring(i);
            }
            if (value.startsWith("=")) {
               value = value.replaceFirst("=","");
            }
         }
      }
      return value;
   }
]]></jsp:declaration>
<jsp:scriptlet><![CDATA[
   String lastUser = (String) application.getAttribute("name");
   if (lastUser==null) {
      lastUser = "Nobody";
      application.setAttribute("name",lastUser);
   }
   String queryString = request.getQueryString();
   String submit = getItem(queryString,"submit");
   if (submit!=null && submit.equals("Submit")) {
   // Collecting the input data
      session.setAttribute("name",getItem(queryString,"name"));
      session.setAttribute("pass",getItem(queryString,"pass"));
      application.setAttribute("name",getItem(queryString,"name"));
      response.sendRedirect("RegDone.jspx?color="
         +getItem(queryString,"color"));
   } else {
   // Presenting the registration form
      out.print("<html><body>");
      out.print("<b>Registration Form</b>:<br/>");
      out.print("<form action=RegForm.jspx method=get>");
      out.print("Login Name:");
      out.print("<input type=text size=16 name=name><br/>");
      out.print("Password:");
      out.print("<input type=text size=16 name=pass><br/>");
      out.print("Favor Color:");
      out.print("<input type=text size=16 name=color><br/>");
      out.print("<input type=submit name=submit value=Submit></br>");
      out.print("</form>");
      out.print("Your session ID is "+session.getId()+"<br/>");
      out.print("Last user on the server: "+lastUser+"<br/>");
      out.print("</body></html>");
   }
]]></jsp:scriptlet>
</jsp:root>

Here is the second JSP page, RegDone.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- RegDone.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<jsp:declaration><![CDATA[
   private String getItem(String queryString, String key) {
      String value = null;
      if (queryString!=null) {
         int i = queryString.indexOf(key);
         if (i>-1) {
            i = i + key.length();
            int j = queryString.indexOf("&",i);
            if (j>-1) {
               value = queryString.substring(i,j);
            } else {
               value = queryString.substring(i);
            }
            if (value.startsWith("=")) {
               value = value.replaceFirst("=","");
            }
         }
      }
      return value;
   }
]]></jsp:declaration>
<jsp:scriptlet><![CDATA[
   out.print("<html><body>");
   String lastUser = (String) application.getAttribute("name");
   String queryString = request.getQueryString();
   out.print("<b>Thank you for registering with us</b>:<br/>");
   out.print("Login Name: "+(String)session.getAttribute("name")
      +"<br/>");
   out.print("Password: "+(String)session.getAttribute("pass")
      +"<br/>");
   out.print("Favor Color: "+getItem(queryString,"color")+"<br/>");
   out.print("Your session ID is "+session.getId()+"<br/>");
   out.print("Last user on the server: "+lastUser+"<br/>");
   out.print("</body></html>");
]]></jsp:scriptlet>
</jsp:root>

See next tutorial on how to test these JSP pages.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

JSP Application Session

 What Is a Session

 The "session" Implicit Object

Passing Values between JSP Pages

 Testing Result of RegForm.jspx

 Using Perl LWP Package for Debugging

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB