Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP form processing


May 12, 2021 JSP


Table of contents


JSP form processing

When we browse the web, we often need to submit information to the server and let the background program handle it. The browser uses the GET and POST methods to submit data to the server.


GET method

The GET method adds the requested encoding information behind the URL, which passes through the URL and the encoding information through the " ? n umber separation. Here's what it looks like:

//www.w3cschool.cn/hello?key1=value1&key2=value2

The GET method is the browser's default method for passing parameters, and some sensitive information, such as passwords, is recommended not to use the GET method.

With get, there is a limit to the size of the transferred data (note that there is a limit to the number of parameters), up to 1024 bytes.


POST method

Some sensitive information, such as passwords, can be passed with the POST method, post submission data is implicit.

POST submission data is not visible, get is passed inside the url (you can look at your browser's address bar).

JSP uses getParameter() to get the passed parameters, and the getInputStream() method handles requests for binary data streams from clients.


JSP reads form data

  • getParameter(): Use the request.getParameter() method to get the value of the form parameters.

  • getParameterValues(): Get data such as the checkbox class (with the same name but multiple values). R eceive array variables, such as checkobx types

  • getParameterNames(): The method can get the name of all variables, and the method returns an Emmeration.

  • getInputStream(): Call this method to read the binary data stream from the client.


An example of a GET method that uses a URL

Here's a simple URL, and use the GET method to pass the parameters in the URL:

http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI

Here's .jsp the JSP program for the main file is used to process the form data submitted by the client, and we use the getParameter() method to get the submitted data:

<html>
<head>
<title>Using GET Method to Read Form Data</title>
</head>
<body>
<center>
<h1>Using GET Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
   <%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last  Name:</b>
   <%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>

Next we access the http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI the output looks like this:

Using GET Method to Read Form Data
First Name: ZARA

Last Name: ALI

Use an example of the form's GET method

Here's a simple HTML form that submits client data to the main file via the GET .jsp file:

<html>
<body>
<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>

Save the HTML code above to the Hello .htm file. P lace the file in the /webapps/ROOT directory. By accessing http://localhost:8080/Hello.htm, the output interface looks like this:

JSP form processing

Fill in the forms "First Name" and "Last Name" and click the "Submit" button, which outputs the results.


Use an instance of the form's POST method

Let's use the POST method to pass form data, modify the .jsp file code that .htm with Hello, as follows:

Main .jsp file code:

<html>
<head>
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<center>
<h1>Using GET Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
   <%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last  Name:</b>
   <%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>

Here's .htm that Hello has modified:

<html>
<body>
<form action="main.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>
</body>
</html>

Accessing the http://localhost:8080/Hello.htm browser, the output is as follows:

JSP form processing

Fill in the forms "First Name" and "Last Name" and click the "Submit" button, which outputs the results.


Pass Checkbox data to the JSP program

Checkbox can pass one or more data.

Here's a simple HTML code and save it in a CheckBox .htm file:

<html>
<body>
<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics"  /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> 
                                                Chemistry
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

Here's .jsp the main file code for working with check box data:

<html>
<head>
<title>Reading Checkbox Data</title>
</head>
<body>
<center>
<h1>Reading Checkbox Data</h1>
<ul>
<li><p><b>Maths Flag:</b>
   <%= request.getParameter("maths")%>
</p></li>
<li><p><b>Physics Flag:</b>
   <%= request.getParameter("physics")%>
</p></li>
<li><p><b>Chemistry Flag:</b>
   <%= request.getParameter("chemistry")%>
</p></li>
</ul>
</body>
</html>

The output of the above examples is:

JSP form processing


Read all form parameters

Below we will use httpservletRequest's getParameterNames() to read all available form parameters, which can take the names of all variables, and the method returns an Emmeration.

Once we have an Enumeration (enumeration), we can call the HasMoreElements() method to determine when to stop using and the nextElement() method to get the name of each parameter.

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>HTTP Header Request Example</title>
</head>
<body>
<center>
<h2>HTTP Header Request Example</h2>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>Param Name</th><th>Param Value(s)</th>
</tr>
<%    Enumeration paramNames = request.getParameterNames();     while(paramNames.hasMoreElements()) {       String paramName = (String)paramNames.nextElement();       out.print("<tr><td>" + paramName + "</td>\n");
      String paramValue = request.getParameterValues(paramName);
      out.println("<td> " + paramValue + "</td></tr>\n");
   }
%>
</table>
</center>
</body>
</html>

Here's .htm Hello file:

<html>
<body>
<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics"  /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

Now let's access Hello.htm file and submit the data through our browser, and the output is as follows:

JSP form processing

You can try reading other forms of data, such as text boxes, turn buttons, or pull-down boxes, using the JSP code above.