English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When we browse web pages, we often need to submit information to the server and let the background program handle it. Browsers use GET and POST methods to submit data to the server.
The GET method adds the encoding information of the request at the end of the URL, and the URL and encoding information are separated by the "?" symbol. As shown below:
http://www.oldtoolbag.com/hello?key1=value1&key2=value2
The GET method is the default method for browsers to pass parameters, and it is recommended not to use the GET method for sensitive information such as passwords.
When using get, the size of the transmitted data is limited (note that it is not the number of parameters that is limited), the maximum being1024bytes.
Sensitive information such as passwords can be passed through the POST method, and POST submission of data is implicit.
POST submission of data is invisible, GET is passed through the URL (you can check your browser's address bar).
JSP uses getParameter() to obtain the parameters passed, and getInputStream() method is used to process the request for the binary data stream from the client.
getParameter(): Use the request.getParameter() method to obtain the value of the form parameters.
getParameterValues(): Obtain data such as checkbox type (names are the same, but values have multiple ones). Receive array variables, such as checkbox type
getParameterNames():This method can obtain the names of all variables, and it returns an Enumeration.
getInputStream():Call this method to read the binary data stream from the client.
The following is a simple URL that uses the GET method to pass parameters in the URL:
http://localhost:8080/testjsp/main.jsp?name=BasicTutorialWebsite&url=http://ww.oldtoolbag.com
testjsp is the project address.
The following is the JSP program in the main.jsp file used to process the form data submitted by the client. We use the getParameter() method to obtain the submitted data:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h1>Read data using the GET method</h1> <ul> <li><p><b>Site name:</b>/b> <%= request.getParameter("name") %> </p></li> <li><p><b>URL:</b>/b> <%= request.getParameter("url") %> </p></li> </ul> </body> </html>
Next, we access http: through the browser//localhost:8080/testjsp/main.jsp?name=JSP tutorial&url=http://ww.w3The output result of codebox.com is as follows:
The following is a simple HTML form that submits client data through the GET method to main.jsp In the file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="GET"> Site name: <input type="text" name="name"> <br /> URL: <input type="text" name="url" /> <input type="submit" value="Submit" /> </form> </body> </html>
Save the above HTML code to the test.htm file. Place the file in the current jsp project's WebContent directory (in the same directory as main.jsp).
By accessing http://localhost:8080/testjsp/test.html Submit form data to the main.jsp file, as shown in the figure below:
Enter information in the two forms "Site name" and "URL", and click the "Submit" button. It will output the result.
Next, let's use the POST method to pass form data, modify the code of main.jsp and Hello.htm files as follows:
The code of main.jsp file:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h1> Use POST method to read data</h1> <ul> <li><p><b>Site name:</b>/b> <% // Solve the problem of Chinese garbled characters String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8); %> <%= name %> </p></li> <li><p><b>URL:</b>/b> <%= request.getParameter("url") %> </p></li> </ul> </body> </html>
In the code we use new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8)to convert encoding and prevent the occurrence of Chinese garbled characters.
The following is the modified code of test.htm:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST"> Site name: <input type="text" name="name"> <br /> URL: <input type="text" name="url" /> <input type="submit" value="Submit" /> </form> </body> </html>
By accessing http://localhost:8080/testjsp/test.html Submit form data to the main.jsp file as follows:
The checkbox 'checkbox' can pass one or even multiple data.
The following is a simple HTML code, and the code is saved in the test.htm file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked"> /> Google <input type="checkbox" name="w3codebox /> Basic Tutorial Website <input type="checkbox" name="taobao" checked="checked"> /> Taobao <input type="submit" value="Select Website"> /> </form> </body> </html>
The above code is displayed in the browser as follows:
The following is the code of the main.jsp file, used to process checkbox data:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h1>Read data from checkboxes</h1> <ul> <li><p><b>Is Google selected:</b> <%= request.getParameter("google")%> </p></li> <li><p><b>Basic Tutorial Website selected:</b> <%= request.getParameter("w3codebox")%> </p></li> <li><p><b>Is Taobao selected:</b> <%= request.getParameter("taobao")%> </p></li> </ul> </body> </html>
By accessing http://localhost:8080/testjsp/test.html Submit form data to the main.jsp file as follows:
after clicking "Select Website":
We will use HttpServletRequest of getParameterNames() to read all form parameters, this method can obtain the names of all variables, and it returns an enumeration.
Once we have an Enumeration, we can call the hasMoreElements() method to determine if there are any more elements, and use the nextElement() method to obtain the name of each parameter.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <h1>Read all form parameters</h1> <table width="100%" border="1" align="center"> <tr bgcolor="#949494"> <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>
The following is the content of the test.htm file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked"> /> Google <input type="checkbox" name="w3codebox /> Basic Tutorial Website <input type="checkbox" name="taobao" checked="checked"> /> Taobao <input type="submit" value="Select Website"> /> </form> </body> </html>
Now we access the test.htm file through the browser to submit data, and the output result is as follows:
By accessing http://localhost:8080/testjsp/test.html Submit form data to the main.jsp file as follows:
After clicking "Select Website":
You can try using the above JSP code to read other objects, such as text boxes, radio buttons, drop-down boxes, and other forms of data.