Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
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.
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.
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.
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 ourFull Stack Development course.