jsp

Pass Parameters to other JSP Page

This is an example of how to pass parameters from one JSP page to another. JavaServer Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. In order to pass parameters from one JSP page to another we have created two JSP pages, as shown below:

  • Caller.jsp uses the jsp:include action, that includes the page Callee.jsp at the time the page is requested. It uses the jsp:param property inside the jsp:include tag to set values to one or more parameters passed to the other page.
  • Callee.jsp page uses the request object that is an instance of a javax.servlet.http.HttpServletRequest object. Each time a client requests a page the JSP engine creates a new object to represent that request. The request object provides methods to get HTTP header information including form data, cookies, HTTP methods. Using the getParameter(String name) it gets the value of a request parameter as a String, here is gets the value of the parameter passed to is by Caller.jsp.

Let’s take a look at the code snippet that follows:  
Caller.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" %>

<html>

<head>
	<title>Java Code Geeks Snippets - Pass Parameters to other JSP Page</title>
</head>

<body>

	This is the caller JSP page.
	
	<jsp:include page="Callee.jsp">
	    <jsp:param name="param1" value="value1"/>
	    <jsp:param name="param2" value="value2"/>
	</jsp:include>

</body>

Callee.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" %>

<html>

<head>
	<title>Java Code Geeks Snippets - Pass Parameters to other JSP Page</title>
</head>

<body>

	This is the callee JSP page.
	
	param1: <%= request.getParameter("param1") %>
	param2: <%= request.getParameter("param2") %>

</body>

URL:

http://myhost:8080/jcgsnippets/Caller.jsp

Output:

This is the caller JSP page. This is the callee JSP page. param1: value1 param2: value2

  
This was an example of how to pass parameters from one JSP page to another in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
John
John
5 years ago

I have common jsp(Ex: test.jsp which has a common code snippet) which is included in all the remaining jsps(Ex Exam1.jsp, Exam2.jsp …..), so how can i pass the objects from Exam1.jsp to test.jsp and Exam2.jsp to test.jsp.

Back to top button