JSP form processing
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.
GET method
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:
http://www.php.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), the maximum is 1024 bytes.
POST method
We can pass some sensitive information, such as passwords, etc. through the POST method. POST submission data is implicit.
POST submitted data is invisible, 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 process the client’s binary data stream request.
JSP reads form data
getParameter(): Use the request.getParameter() method to get the value of the form parameter.
getParameterValues(): Obtain 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, and this method returns an Emumeration.
getInputStream():Call this method to read the binary data stream from the client.
Example of using the GET method of the URL
The following is a simple URL and uses the GET method to pass the parameters in the URL:
http://localhost:8080/testjsp/main.jsp?name=php中文网&url=http://ww.php.cn
testjsp is the project address.
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:
<%@ 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>php中文网(php.cn)</title> </head> <body> <h1>使用 GET 方法读取数据</h1> <ul> <li><p><b>站点名:</b> <%= request.getParameter("name")%> </p></li> <li><p><b>网址:</b> <%= request.getParameter("url")%> </p></li> </ul> </body> </html>
Next we use the browser Visit http://localhost:8080/testjsp/main.jsp?name=php Chinese website&url=http://ww.php.cnThe output results are as follows:
Example of using the GET method of the form
The following is a simple HTML form that submits client data through the GET method To the main.jsp file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="main.jsp" method="GET"> 站点名: <input type="text" name="name"> <br /> 网址: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
Save the above HTML code to the test.htm file. Place this file in the WebContent directory of the current jsp project (the same directory as main.jsp).
Submit the form data to the main.jsp file by visiting http://localhost:8080/testjsp/test.html. The demo Gif is as follows:
Fill in the information in the "Site Name" and "Website" forms and click the "Submit" button, it will output the results.
Using the POST method example of the form
Next, let us use the POST method to transfer the form data and modify the main.jsp and Hello.htm file codes as follows:
main.jsp file code:
<%@ 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>php中文网(php.cn)</title> </head> <body> <h1>使用 POST 方法读取数据</h1> <ul> <li><p><b>站点名:</b> <% // 解决中文乱码的问题 String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8"); %> <%=name%> </p></li> <li><p><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 the encoding to 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>php中文网(php.cn)</title> </head> <body> <form action="main.jsp" method="POST"> 站点名: <input type="text" name="name"> <br /> 网址: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
By accessing http://localhost:8080/testjsp/test.html Submit the form data to the main.jsp file. The demo Gif is as follows:
Transfer Checkbox data to the JSP program
Checkbox checkbox One or more data can be passed.
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>php中文网(php.cn)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked" /> Google <input type="checkbox" name="php" /> php中文网 <input type="checkbox" name="taobao" checked="checked" /> 淘宝 <input type="submit" value="选择网站" /> </form> </body> </html>
The above code will look like this when accessed by the browser:
The following is main.jsp file code, used to process check box 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>php中文网(php.cn)</title> </head> <body> <h1>从复选框中读取数据</h1> <ul> <li><p><b>Google 是否选中:</b> <%= request.getParameter("google")%> </p></li> <li><p><b>php中文网是否选中:</b> <%= request.getParameter("php")%> </p></li> <li><p><b>淘宝是否选中:</b> <%= request.getParameter("taobao")%> </p></li> </ul> </body> </html>
Submit the form by visiting http://localhost:8080/testjsp/test.html Data is sent to the main.jsp file, and the demo Gif is as follows:
Read all form parameters
We will use below HttpServletRequestgetParameterNames() to read all form parameters. This method can get the names of all variables. This method returns an enumeration.
Once we have an Enumeration, we can call the hasMoreElements() method to determine if there are any more elements, and the nextElement() method to get 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>php中文网(php.cn)</title> </head> <body> <h1>读取所有表单参数</h1> <table width="100%" border="1" align="center"> <tr bgcolor="#949494"> <th>参数名</th><th>参数值</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>php中文网(php.cn)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked" /> Google <input type="checkbox" name="php" /> php中文网 <input type="checkbox" name="taobao" checked="checked" /> 淘宝 <input type="submit" value="选择网站" /> </form> </body> </html>
Now we access the test.htm file through the browser to submit the data, and the output results are as follows:
By accessinghttp://localhost:8080/testjsp/test.html Submit the form data to the main.jsp file. The demonstration Gif image 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.