How to send values from one JSP to another and then read it?


To send value from one JSP to another and read it you have to make a form in the 1st page and give the file name of another JSP in form action and use the method post. 

To send Value

If  a.jsp is first page and b.jsp is another page and you have to send data from a.jsp to b.jsp then you have to write b.jsp in form action and use method post.

For Example:
<form action="b.jsp" method="post">

To Read Value

Now in b.jsp you have to use request.getParameter("name_of_input_type") to read the value of the input field of a.jsp.

For Example:
a.jsp contain input type "mail"
<input type="email" name="mail" placeholder="Enter your Email"/>

then to read this input type in b.jsp you have to make a string variable and use request.getParameter("name_of_input_type") to get value.

For Example:
a.jsp
<input type="email" name="mail" placeholder="Enter your Email"/> //name of this input type is mail

b.jsp
<%
    String mail=request.getParameter("mail"); //put the name of the input type in between the double                                                                                     quotes
%>

Full Example:

a.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form action="b.jsp" method="POST">
    <input type="text" name="username" placeholder="Enter your name"/>
    <input type="email" name="mail" placeholder="Enter your Email"/>
    <input type="number" name="phone" placeholder="Enter your Phone Number"/>
  </form>
</body>
</html>

b.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <%
    String name=request.getParameter("username");
    String mail=request.getParameter("mail");
    String phone=request.getParameter("phone");

    System.out.println(name); //print value of name
    System.out.println(mail); //print value of mail
    System.out.println(phone); //print value of phone
  %>
</body>
</html>

Post a Comment

0 Comments