When we browse the web, we often need to submit information to the server and let the background program process it. The browser uses the GET and POST methods to submit data to the server.
The GET method adds the requested encoding information after the URL, and the URL and encoding information are separated by a "?" symbol. As shown below:
//www.w3cschool.cn/hello?key1=value1&key2=value2
The GET method is the default method for browsers to pass parameters. It is recommended not to use the GET method for some sensitive information, such as passwords.
When using get, the size of the transmitted data is limited (note that the number of parameters is not limited), and the maximum is 1024 bytes.
We can pass some sensitive information, such as passwords, through the POST method, and post submission data is implicit.
The data submitted by POST is invisible, and GET is passed in the URL (you can look at the address bar of your browser).
JSP uses getParameter() to obtain the passed parameters, and the getInputStream() method is used to handle the client's binary data stream request.
getParameter(): Use the request.getParameter() method to get the value of the form parameter.
getParameterValues(): Get data such as checkbox class (same name, but multiple values). Receive array variables, such as checkbox type
getParameterNames(): This method can get the names of all variables. This method returns an Emumeration.
getInputStream(): Call this method to read the binary data stream from the client.
The following is a simple URL and uses the GET method to pass the parameters in the URL:
http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI
The following is the JSP program of the main.jsp file for processing form data submitted by the client. We use the getParameter() method to obtain 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 http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI through the browser. The output results are as follows:
Using GET Method to Read Form DataFirst Name: ZARALast Name: ALI
The following is a simple HTML form that submits client data to the main.jsp file via the GET method:
<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 above HTML code into the Hello.htm file. Place this file in the /webapps/ROOT directory. By accessing http://localhost:8080/Hello.htm , the output interface is as follows:
Fill in the information in the "First Name" and "Last Name" forms and click the "Submit" button, it will output the results.
Next, let us use the POST method to transfer form data and modify the main.jsp and Hello.htm file codes 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>
The following is the modified code of Hello.htm:
<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>
Access http://localhost:8080/Hello.htm through the browser, the output is as follows:
Fill in the information in the "First Name" and "Last Name" forms and click the "Submit" button, it will output the results.
Checkbox checkbox can pass one or even multiple data.
The following is a simple HTML code and save the code in the 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>
The following is the main.jsp file code, used to process 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 result of the above example is:
Below we will use HttpServletRequest's getParameterNames() to read all available form parameters. This method can obtain the names of all variables. This method returns an Emumeration.
Once we have an Enumeration, we can call the hasMoreElements() method to determine when to stop using it 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 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.getParameter(paramName); out.println("<td> " + paramValue + "</td></tr>n"); }%></table></center></body></html>
The following is the content of the Hello.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" /> Chem<input type="submit" value="Select Subject" /></form> </body></html>
Now we access the Hello.htm file through the browser and submit the data. The output is as follows:
You can try to use the above JSP code to read other objects, such as text boxes, radio buttons or drop-down boxes, and other forms of data.