When we browse a web page, we often need to submit information to the server and let the background program process it. The browser uses GET and POST methods to submit data to the server.
Get method - The default way for the server to pass parameters, some sensitive information, such as passwords, etc. are not recommended to use get method.
When using get, the size of the transmitted data is limited to 1024 bytes.
Post method - Some sensitive information, such as passwords, can be passed through the post method. Post submission data is implicit.
post is invisible when submitting data, and get uses what is passed in the url.
JSP uses getParameter() to get the passed parameters, and getInputStream() to process the client's request for binary data exchange.
JSP reads form data
- getParameter(): Use the request.getParameter() method to get the value of the parameter
- getParameterNames(): This method takes the names of all variables and returns an Emumeration.
- getParameterValues(): Get data such as the checkbox class (with the same name but multiple values). Accept array variables
- getInputStream(): Call this method to get the binary data stream from the client
Use Url's get method
main.jsp
<form action="date.jsp" method="GET"> site name: <input type="text" name="name"> <br/> website address: <input type="text" name="url"/><input type="submit" value="submit"/> </form>.</body></html><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <pre style="" class="prettyprint prettyprinted"><span class="tag"> <h1></span><span class="pln">Use GET Method to read data</span><span class="tag"></h1></span><span class="pln"></span>
data.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title> My JSP'date.jsp'starting page</title> </head> <body> <h1> Read the data by GET method </h1> <ul> <li> <p> <b> Site name: </b> <%= getParameter ("request")%>.</p></li></body></html>
Examples of POST methods using forms
main.jsp
date.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="date.jsp" method="post"> //Site name: <input type= "text" name= "name"> <br /> //Website: <input type="text" name="url"/> <input type="submit" value="Submission" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <h1>Use POST Method to read data</h1> <ul> <li><p><b>Site name:</b> <% // Solving the Problem of Chinese Scrambling /* String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8"); */ %> <%=request.getParameter("name")%> </p></li> <li><p><b>Website:</b> <%= request.getParameter("url")%> </p></li> </ul> </body> </html>
Transfer CheckBox data to jsp program
Checkbox checkbox can pass one or more data.
main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <!-- check box checkbox One or more data can be transmitted. --> <form action="date.jsp" method="post" target="1"> <input type="checkbox" name="google" checked="checked" /> Google <input type="checkbox" name="runoob" checked="checked"/> Rookie tutorial <input type="checkbox" name="taobao" checked="checked" /> taobao <input type="submit" value="Select website" /> </form> </body> </html>
date.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'date.jsp' starting page</title> &l� ��������26gt; <body> <!-- Read all form parameters --> <h1>Read all form parameters</h1> <table width="100%" border="1" align="center"> <tr bgcolor="#949494"> <th>Parameter name</th><th>parameter values</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> <!-- Read data from check boxes --> <h1>Read data from check boxes</h1> <ul> <li><p><b>Google Checked:</b> <%= request.getParameter("google")%> </p></li> <li><p><b>Whether or not the novice course is selected:</b> <%= request.getParameter("runoob")%> </p></li> <li> <p><b>Is Taobao Selected:</b> <%= request.getParameter("taobao")%> </p> </li> </ul> </body> </html>
Reproduced in: https://my.oschina.net/u/3389779/blog/888878