Table of contents
1.
Introduction
2.
What is Form processing in JSP?
3.
Methods in Form Processing
3.1.
GET Method
3.2.
POST Method
4.
Methods used in reading data from a form using JSP
5.
Example: GET Method using URL
6.
Example: GET Method using the form
7.
Example: POST Method using the form in JSP
8.
Example: Passing checkboxData and processing it
9.
Example: Reading all Form Parameter in JSP
10.
Frequently Asked Questions
10.1.
What is the full form of JSP?
10.2.
What is JSP and its uses?
10.3.
How a JSP page is executed?
10.4.
What is conditional processing in JSP?
10.5.
Can we use if-else on the JSP page?
11.
Conclusion
Last Updated: May 4, 2024

JSP Form processing

Author Aryan Raj
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSP(JavaServer Pages ) is a Sun Microsystems specification for combining Java with HTML to provide dynamic content for Web pages. 

When you create dynamic content, JSPs allow you to add Java code directly into your HTML code, in contrast with HTTP servlets, in which you add HTML inside Java code.

JSP is a server-side programming language that combines HTML, XML, Java Servlet, and JavaBeans technologies into one highly productive technology. It allows developers to develop reliable, high-performance, platform-independent web applications and dynamic websites. 

JSP - Database Access

What is Form processing in JSP?

You must come across many situations when you have to pass some information from your browser (take input from a browser) to the WebServer and eventually to your backend program. 

The browser uses two methods to pass this information to the webserver. These are the GET Method and POST Method. 

 

Form processing in JSP

Methods in Form Processing

There are two commonly used methods to send and receive back information to the webserver.

GET Method

The GET method is the default method to send information from the browser to the webserver. 

It sends the user information to the page request separated by the ? character. 

Example: http://www.abc.com/helloworld?key1=v1&key2=v2

It is not suggested to use the GET method if you have some sensitive information or password to pass to the server because it produces a long string that appears in your browser's address bar.

Because of the size limitation, we can only send 1024 characters in the request.

 

POST Method

The POST method is a generally more reliable method of passing information to a back-end program.

This method packages the information exactly as the GET method, but instead of sending it as a text form after a ? in the URL, it sends it as a separate message. This message comes to the backend program in the form of the standard input, which you can parse and use for your processing.

JSP handles this type of request using the getParameter() method to read simple parameters and the getInputStream() method to read binary data streams coming from the client.

Methods used in reading data from a form using JSP

The following methods are used to handle form processing in JSP:

getParameterValues()This method returns multiple values if the specified parameter appears more than once and returns multiple values.
getParameterNames() It is used to get the name of parameters if you want a complete list of all parameters in the current request.
getParameter()It is used to get the value of a form parameter.
getInputStream()It is used to read binary data streams coming from the client.

 

Example: GET Method using URL

To pass the information using URL, we will use the getParameter() method, which makes it very easy to access the passed information. Following is the program of main.jsp that will handle the input.

localhost:8080/main.jsp?first_name=Ninja&last_name=hattodi 

 

main.jsp

<!DOCTYPE html>
<html>
   <head>
      <title> GET Method using URL</title>
   </head>
   
   <body>
      <h1>Coding Ninjas</h1>
     
        <p><b>First Name:</b>
            <%= request.getParameter("first_name")%>
        </p>
        <p><b>Last Name:</b>
            <%= request.getParameter("last_name")%>
        </p>
   </body>
</html>

 

Output:

Example: GET Method using the form

In this example, we have two files:

One is our Form(index.html) that takes the input data, and the other is our JSP file(main.jsp) that reads and processes. Inside our form, we have two fields, “First Name” and “Last Name” with a submit button through which we will process the action to another JSP page. We will fetch the input using the getParameter method. With the help of submit button, we will pass the field values into the main.jsp page.

 

index.html

<!DOCTYPE html>
<html>
<body>
    <h1>Coding Ninjas</h1>
    <form action = "main.jsp" method = "GET">
        First Name: <input type = "text" name = "first_name">
        <br />
        Last Name: <input type = "text" name = "last_name" />
        <input type = "submit" value = "Submit" />
     </form>
</body>
</html>

 

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
 
 <!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=UTF-8">
 <title>Insert title here</title>
 </head>
    <body >
        <h1>Coding Ninjas</h1>
        <p><b>First Name:</b></p>
        <%=request.getParameter("first_name")%>
        <br/>
        <p><b>Last Name:</b></p>
        <%=request.getParameter("last_name")%>
    </body>
 </html>

 

Output:

After submitting your details(First Name and Last Name), you will get the following output.

Example: POST Method using the form in JSP

POST method is similar in programming to the GET method. Using the POST method, the only thing that changes is that our data goes as a separate message rather than an appended message in the URL.

Let’s see the program. We have two files, one Form(index.html) file to take the information from the user and another JSP file(post.jsp) to handle that form data.

 

index.html

<!DOCTYPE html>
<html>
<body>
    <h1>Coding Ninjas</h1>
    <form action = "post.jsp" method = "POST">
        First Name: <input type = "text" name = "first_name">
        <br />
        Last Name: <input type = "text" name = "last_name" />
        <input type = "submit" value = "Submit" />
     </form>
 </form>
</body>
</html>

 

post.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
 
 <!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=UTF-8">
 <title>Insert title here</title>
 </head>
    <body>
        <h1>Coding Ninjas</h1>
        <p><b>First Name:</b></p>
        <%=request.getParameter("first_name")%>
        <br/>
        <p><b>Last Name:</b></p>
        <%=request.getParameter("last_name")%>
    </body>
 </html>

 

Output:

This index.html form file will take the input from the user and pass the action to the post.jsp file to handle the data.

 

Example: Passing checkboxData and processing it

In this example, we will read and process the checkbox items i.e. multiple values. We have created a box.html file to input the data and a box.jsp file to process that data. We use the POST method and call the checkbox.jsp page.

box.html

<!DOCTYPE html>
<html>
<head>
<title>Processing Checkbox data</title>
</head>
<body>
  <form action="checkbox.jsp" method="POST" target="_blank">
  <input type="checkbox" name="Monday" checked="checked" /> Monday</br>
  <input type="checkbox" name="Tuesday" /> Tuesday</br>
  <input type="checkbox" name="Wednesday" /> Wednesday</br>
  <input type="checkbox" name="Thrusday"checked="checked" /> Thrusday</br>
  <input type="checkbox" name="Friday" checked="checked" /> Friday</br>
  <input type="checkbox" name="Saturday" checked="checked" /> Saturday</br>
  <input type="checkbox" name="Sunday" checked="checked" /> Sunday</br>
  <input type="submit"value="Select Day" />
  </form>
</body>
</html>

 

box.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>Working Days</h1>
 <ul>
  <li>
        <p>
            <b>Monday:</b>
            <%=request.getParameter("Monday")%>
        </p>
  </li>
    <li>
        <p>
            <b>Tuesday:</b>
            <%=request.getParameter("Tuesday")%>
        </p>
    </li>
    <li>
        <p>
            <b>Wednesday:</b>
            <%=request.getParameter("Wednesday")%>
        </p>
    </li>
    <li>
        <p>
            <b>Thrusday:</b>
            <%=request.getParameter("Thrusday")%>
        </p>
    </li>
    <li>
        <p>
            <b>Friday:</b>
            <%=request.getParameter("Friday")%>
        </p>
    </li>
    <li>
        <p>
            <b>Saturday:</b>
            <%=request.getParameter("Saturday")%>
        </p>
    </li>
    <li>
        <p>
            <b>Sunday:</b>
            <%=request.getParameter("Sunday")%>
        </p>
    </li>
 </ul>
</body>
</html>

 

Output:

Further action will be taken by the checkbox.jsp file to process this information,

Example: Reading all Form Parameter in JSP

We can read the complete form data using the Enumeration for all parameter values and parameter names(Using this method returns an Enumeration that contains the parameter names in an unspecified order). Then, we call an enumeration of all parameter names. Using while loop on parameter names, we can get all values for all the parameters using hasMoreElements()

In this example, we have taken the output in a tabular format.

 

box.html

<!DOCTYPE html>
<html>
<head>
<title>Processing Checkbox data</title>
</head>
<body>
  <form action="checkbox.jsp" method="POST" target="_blank">
  <input type="checkbox" name="Monday" checked="checked" /> Monday</br>
  <input type="checkbox" name="Tuesday" /> Tuesday</br>
  <input type="checkbox" name="Wednesday" /> Wednesday</br>
  <input type="checkbox" name="Thrusday"checked="checked" /> Thrusday</br>
  <input type="checkbox" name="Friday" checked="checked" /> Friday</br>
  <input type="checkbox" name="Saturday" checked="checked" /> Saturday</br>
  <input type="checkbox" name="Sunday" checked="checked" /> Sunday</br>
  <input type="submit"value="Select Day" />
  </form>
</body>
</html>

 

checkbox.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 form in JSP</h2>
 <table width="50%" border="2">
    <tr>
        <th>Weak Days</th>
        <th>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:

After clicking on select day you get the output as given below:

You can also read about the JSP Architecture and Lifecycle JSP HTTP Code

Frequently Asked Questions

What is the full form of JSP?

JSP stands for Java Server Pages.

What is JSP and its uses?

JSP is a collection of technologies used by software developers to create dynamically generated web pages based on HTML, XML, SOAP, or other document types.

How a JSP page is executed?

JSP pages are executed by a JSP engine, which translates the JSP tags into HTML code, then it is sent to the client's browser.

What is conditional processing in JSP?

Conditional processing is a bunch of code that reacts to specified conditions and acts differently based on those conditions. When application behavior depends on unknown data at the time of development, such as query information or user data, conditional processing is an essential tool.

Can we use if-else on the JSP page?

The “If else” statement is the basis of all control flow statements. It tells the program to execute a certain section of code only if the particular test evaluates to true. This condition is used to test whether they are true or false for more than one condition.

Conclusion

This blog discussed how the form data is taken and processed. The information is taken from the browser and sent to the webserver, which is finally sent to the backend programming.

We also discussed different methods and their applications as well.

If you are pursuing a new career in Web Development, we suggest you get your fundamentals crystal clear with our Full Stack Development course

Happy Learning, Ninja!

This course will help you!

Live masterclass