Table of Contents
Pass values from one jsp to another jsp file:
Form values from one jsp can be sent to another jsp file with action parameter in form and getParameter method in the second jsp file.
Things to remember:
- Mention the jsp2.jsp file in the form action of jsp1.jsp file.
- In jsp2.jsp file use getParameter() method and pass the name of the form element inside to get the value whichever is passed from jsp1.jsp file.
jsp1.jsp file:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>First JSP Page</title> </head> <body> Enter your Name here: <form action="jsp2.jsp" method="POST"> <input type="text" name="name" id="name"/> <input type="submit" value="Pass Name to Jsp2.jsp"/> </form> </body> </html>
jsp2.jsp file:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Second JSP Page</title> </head> <body> <% String enteredName = request.getParameter("name"); out.println(enteredName); %> </body> </html>
Output: