How to process form data in JSP

In the previous article, we have seen an HTTP Server response in JSP. In this article, we will cover How Form Data is processed in JSP.

As we know When we sent information from the browser to the server. In order to send it, the browser uses two important methods which are GET and POST. In this article, we will discuss the same.

What is Form Data?

When we sent information from the browser to the webserver the browser uses methods that are provided in the <form> tag element. It uses the following two methods.

  • GET
  • POST

What is GET in JSP?

The GET method is the method used by the client to ask the server to send back a given resource.

  • The GET is a method that sends the information appended to the user request.
  • In the following example, we see the information like key1 & key2 is passed along with the request.
  • The data is appended to the URL as a series of name/value pairs.
  • The GET method is a default method.
  • The GET method is used to retrieve data from the browser.
http://www.abc.com/welcome?key1=value1&key2=value2

What is Post in Servlet?

The POST method is the method the browser uses to talk to the server when asking for a response.

  • The POST is a method that sends the information from browser to server and is more reliable as compared to GET.
  • In the following example, the information passed is the same as that of getting the method only difference here is that instead of sending string after? it sends as a separate message.
  • When we submit the form with the POST method, we get no data appended to the URL.
http://wwww.abc.com/

Methods to Read Form data using JSP

  • getParameter(): This method returns the value of the form.
  • getParameterValues():This method returns multiple values.
  • getParameterNames():This method return complete list of parameter name in the request.

Let’s see the example of the GET method

In this example, we will use the GET method. To fetch the value we will use the getParameter() method.

index.jsp

<%@ 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>Insert title here</title>
</head>
<body>
  <form action="getForm.jsp" method="get">
    <table align="center" bgcolor="#B7CEEC">

      <tr>
        <td>Name</td>
        <td><input type="text" name="name"></td>
      </tr>

      <tr>
        <td>Email</td>
        <td><input type="text" name="email"></td>
      </tr>

      <tr>
        <td>Class</td>
        <td><select name="class">
            <option value="FE">FE</option>
            <option value="SE">SE</option>
            <option value="TE">TE</option>
            <option value="BE">BE</option>
        </select></td>
      </tr>

      <tr>
        <td>Comment</td>
        <td><textarea name="msg" rows="5" cols="30"></textarea></td>
      </tr>

      <tr>
        <td><input type="submit" value="Done"></td>
      </tr>

    </table>



  </form>
</body>
</html>

getForm.jsp

<%@ 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>Insert title here</title>
</head>
<body>
  <%
out.print("<h3>USING GET</h3>");
String name=request.getParameter("name");
String email=request.getParameter("email");
String class1=request.getParameter("class");
String comment=request.getParameter("msg");
out.print("<table>");
out.print("<tr><td>Name is: </td><td>"+name+"</tr>");
out.print("<br>");
out.print("<tr><td>Email is: </td><td>"+email+"</tr>");
out.print("<tr><td>Class is :</td><td>"+class1+"</tr>");
out.print("<tr><td>Comment is :</td><td>"+comment+"</tr>");
%>
</body>
</html>

Output

Let’s see the example of the POST method

In this example, we will use the POST method. To fetch the value we will use the getParameter() method and also we will see how JSP handles the checkbox button.

newindex.jsp

<%@ 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>Insert title here</title>
</head>
<body>
  <form action="newForm.jsp" method="post">
    <table align="center" bgcolor="#B7CEEC">
      <tr>
        <td>Name :</td>
        <td><input type="text" name="name"></td>
      </tr>

      <td>Roll No :</td>
      <td><input type="text" name="roll"></td>
      </tr>

      <tr>
        <td>Select:</td>
        <td><input type="checkbox" name="c" checked="checked" /> C <input
          type="checkbox" name="cpp" /> C++ <input type="checkbox"
          name="java" checked="checked" /> Java</td>
      </tr>
      <tr>
        <td><input type="submit" value="Done" /></td>
      </tr>


    </table>

  </form>
</body>
</html>

newForm.jsp

<%@ 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>Insert title here</title>
</head>
<body>
  <%
out.print("<h3>USING POST</h3>");
String name=request.getParameter("name");
String c=request.getParameter("c");
String cpp=request.getParameter("cpp");
String java=request.getParameter("java");
out.print("<table>");
out.print("<tr><td>Name is: </td><td>"+name+"</tr>");
out.print("<tr><td>Subject1 is: </td><td>"+c+"</tr>");
out.print("<tr><td>Subject2 is: </td><td>"+cpp+"</tr>");
out.print("<tr><td>Subject3 is: </td><td>"+java+"</tr>");
out.print("<br>");

%>
</body>
</html>

Output

Thus, we have seen How Form data is processed in JSP using some methods like getParameter() and so on.

In the next article of this tutorial, we will discuss Filters in JSP i.e How to write filters in JSP, Its advantages.