JSP Form Processing

Form Processing in JSP

In this article, I am going to discuss Form Processing in JSP with Examples. Please read our previous article where we discussed HTTP Status Codes. At the end of this article, you will understand the following pointers.

  1. What is Form Processing in JSP?
  2. Methods in Form Processing
  3. Understanding the GET and POST methods
  4. Reading Form Data using JSP
  5. GET Method Example Using URL
  6. GET Method Example Using Form
  7. POST-Method Example Using Form
  8. Passing Form Data to JSP Program
  9. Reading All Form Parameters
Form Processing in JSP

When we need to pass information from our browser to the web server, form processing the most common method for web processing. It is the common method that helps us to interact with the web pages which became very easy with the help of JSP. There are two ways through which a browser can send the data to the server:

  1. GET: It is used to get a resource or data from the server.
  2. POST: It is used to submit or post data to the server for processing.
GET Method

It is the default method used to pass information from the browser to the webserver. It sends the encoded user information to the page request separated by the “?” character. We can only send 1024 characters in the request as it has a size limitation. It is not recommended to use the GET method if you have the password or other sensitive information to pass to the server because it produces a long string that appears in your browser.

POST Method

It is a more reliable method used to pass information to a backend program. It sends information as a separate message instead of sending it as a text string after a “?” in the URL. It is recommended to use the GET method if you have the password or other sensitive information to pass to the server because it produces a long string that appears in your browser. The information comes as standard input to the backend program which we can use for processing.

Methods to Handle Form Data Processing in JSP
  1. getParameter(): It is used to get the value of a form parameter.
  2. getParameterValues(): It is used to return multiple values if the parameter appears more than once and return multiple values.
  3. getParameterNames(): It is used to get the name of parameters if you want a complete list of all parameters in the current request.
  4. getInputStream(): It is used to read binary data stream coming from the client.
Example of the GET Method using URL in JSP

In this example, we have passed two values using the GET method. GetUrl.jsp is the file used to handle input given by the web browser. We have used the getParameter() method which is easy to access the passed information.

GetUrl.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>GET using URL</title>
</head>
<body>
 <h1>Form Processing</h1>
 <p>
  <b>UserName :</b>
  <%=request.getParameter("username")%>
  <br /> <b>Password :</b>
  <%=request.getParameter("password")%>
 </p>
</body>
</html>

Output

Type http://localhost:8007/HelloWorldJSP/GetUrl.jsp?username=Manisha&password=Manisha to get the output:

Example of the GET Method using URL in JSP

Example of the GET Method using FORM in JSP

In this example, in GetForm.jsp we have used a form with two fields “username” and “password” with a submit button through which we have processed the action to another JSP page. We have fetched the input using the getParameter method. The submit button helps us to pass the field values into another GetFormProcess.jsp JSP page. In GetFormProcess.jsp pages, we get the values using the request object’s getParemeter method.

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>Form Processing</title>
</head>
<body>
 <form action="GetFormProcess.jsp" method="GET">
  UserName: <input type="text" name="username"> <br /><br/>
  Password: <input type="text" name="password" ><br/><br/> <input type="submit"
   value="Submit" />
 </form>
</body>
</html>
GetFormProcess.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>
 <h1>Form Processing</h1>
 <p>
  <b>Welcome User :</b>
  <%=request.getParameter("username")%>
  <br/>
  <b>Your Password is :</b>
  <%=request.getParameter("password")%>
 </p>
</body>
</html>
Output

Run your code to get the following output. Enter your details and click submit button.

Example of the GET Method using FORM in JSP

After clicking on the submit button, you will get the following output.

Form Processing in JSP

Example of POST method using FORM in JSP

In this example, we have created two files. PostForm.jsp file is to take information from the user and PostFormProcess.jsp file is to handle the data of PostForm.jsp file. PostForm.jsp will take input from the user and pass the action to the PostFormProcess.jsp file to handle the data.

PostForm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
 <form action="PostFormProcess.jsp" method="POST">
  Username: <input type="text" name="username"> <br /> <br/>
  Password: <input type="text" name="password" ><br/><br/> <input type="submit"
   value="Submit" />
 </form>
</body>
</html>
PostFormProcess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Using POST Method</title>
</head>
<body>
 <h1>Form Processing</h1>
 <b>Username:</b>
 <%=request.getParameter("username")%>
 </br>
 <b>Password:</b>
 <%=request.getParameter("password")%>
</body>
</html>
Output

Run your code to get the following output. Enter your details and click submit.

Example of POST method using FORM in JSP

After submitting your details you will get the following output.

Form Processing in JSP with Examples

Passing Checkbox data and process it

In this example, we are reading and processing the checkbox items with multiple values. We have created a checkbox.jsp file to input the data and a checkboxProcess.jsp file to process that data. Here, checkbox.jsp uses the POST method and calls the checkboxProcess.jsp page.

checkbox.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Processing Checkbox data</title>
</head>
<body>
 <form action="checkboxProcess.jsp" method="POST" target="_blank">
  <input type="checkbox" name="Maths" checked="checked" /> Maths</br> <input
   type="checkbox" name="Physics" /> Physics</br> <input type="checkbox"
   name="Chemistry" /> Chemistry</br> <input type="checkbox" name="English"
   checked="checked" /> English</br> <input type="checkbox" name="Computers"
   checked="checked" /> Computers</br> <input type="submit"
   value="Select Subject" />
 </form>
</body>
</html>
checkboxProcess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Reading Checkbox Data</title>
</head>
<body>
 <h1>Reading Checkbox Data filled in form</h1>
 <ul>
  <li><p>
    <b>Maths Checkbox:</b>
    <%=request.getParameter("Maths")%>
   </p></li>
  <li><p>
    <b>Physics Checkbox:</b>
    <%=request.getParameter("Physics")%>
   </p></li>
  <li><p>
    <b>Chemistry Checkbox :</b>
    <%=request.getParameter("Chemistry")%>
   </p></li>
  <li><p>
    <b>English Checkbox:</b>
    <%=request.getParameter("English")%>
   </p></li>
  <li><p>
    <b>Computers Checkbox:</b>
    <%=request.getParameter("Computers")%>
   </p></li>
 </ul>
</body>
</html>
Output

Passing Checkbox data and process it

Once you select the checkboxes and click on the Select Subject button, you will get the following output.

JSP Form Processing

Reading All Form Parameters in JSP

In this example, we can read the complete form data at once using the Enumeration for all parameter names and values. For which we are calling an Enumeration of all parameter names. Then, we can simply get all the values for all the parameters using hasmoreelements() by using a while loop on parameter names. And then we are fetching the output in tabular format.

checkbox.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Processing Checkbox data</title>
</head>
<body>
 <form action="checkboxProcess.jsp" method="POST" target="_blank">
  <input type="checkbox" name="Maths" checked="checked" /> Maths</br> <input
   type="checkbox" name="Physics" /> Physics</br> <input type="checkbox"
   name="Chemistry" /> Chemistry</br> <input type="checkbox" name="English"
   checked="checked" /> English</br> <input type="checkbox" name="Computers"
   checked="checked" /> Computers</br> <input type="submit"
   value="Select Subject" />
 </form>
</body>
</html>
checkboxProcess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<title>Reading all parameters</title>
</head>
<body>
 <h2>Reading all parameters in the form</h2>
 <table width="50%" border="2">
  <tr>
   <th>Parameter Name</th>
   <th>Parameter Value</th>
  </tr>
  <%
   Enumeration paramNames = request.getParameterNames();
  while (paramNames.hasMoreElements()) {
   String paramName = (String) paramNames.nextElement();
   out.print("<tr><td>" + paramName + "</td>\n");
   String paramValue = request.getParameter(paramName);
   out.println("<td> " + paramValue + "</td></tr>\n");
  }
  %>
 </table>
</body>
</html>
Output

Reading All Form Parameters in JSP

Once you select the checkboxes and click on the select subject button, you will get the following output.

JSP Form Processing with Examples

In the next article, I am going to discuss Filters in JSP. Here, in this article, I try to explain JSP Form Processing with Examples. I hope you enjoy this Form Processing in the JSP article.

Leave a Reply

Your email address will not be published. Required fields are marked *