Latest web development tutorials

JSP form processing

We browse the Web, often you need to submit information to the server, and let the daemon process. The browser uses the GET and POST method to submit data to the server.


GET Method

GET method of encoding information request to add to the URL, URL encoding information through the "?" Separated by. As follows:

http://www.w3big.com/hello?key1=value1&key2=value2

GET method is a method of passing parameters default browser, sensitive information, such as passwords and other recommended not to use the GET method.

Get used, the size of the transmission data is limited (note the number of parameters is not restricted), up to 1024 bytes.


POST method

Sensitive information such as passwords and so we can pass the POST method, POST to submit data is implicit.

POST to submit data is not visible, GET is passed through the inside url (you can look at your browser's address bar).

JSP using the getParameter () to get the passed parameter, getInputStream () method is used to process the request binary data stream of clients.


JSP form data is read

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

  • getParameterValues (): get as checkbox class (the same name, but a plurality of values) data.Receiving an array of variables, such as type checkbox

  • getParameterNames (): This method can obtain the names of all the variables, the method returns a Emumeration.

  • getInputStream (): This method is called to read the binary data stream from the client.


Examples of use of the URL GET method

The following is a simple URL, and uses the GET method to pass parameters in the URL:

http://localhost:8080/testjsp/main.jsp?name=本教程&url=http://ww.w3big.com

testjsp address for the project.

The following is the JSP program main.jsp file for processing form data submitted by the client, we use the getParameter () method to get the data submitted:

<%@ 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>本教程(w3big.com)</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 visit the browser http: // localhost: 8080 / testjsp / main.jsp name = This tutorial & url = http:? //ww.w3big.com Output results are as follows:


Use the form GET method of Example

The following is a simple HTML form, which will be submitted by the GET method to main.jsp client data file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</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 into test.htm file. Place the file in the current directory under WebContent jsp project (with main.jsp the same directory).

By visiting http: // localhost: 8080 / testjsp / test.html main.jsp submit the form data to a file, Gif demo shown below:

Fill in the information in the "site name" and "URL" two form, and click the "Submit" button, it will output the results.


Examples of the form's POST method

Let's use the POST method to transmit the form data, and modify main.jsp Hello.htm file code 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>本教程(w3big.com)</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>

Code we use new String ((request.getParameter ( "name ")). GetBytes ( "ISO-8859-1"), "UTF-8") to convert code, prevent the occurrence of Chinese garbled.

Here is the code test.htm modified:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</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 visiting http: // localhost: 8080 / testjsp / test.html main.jsp submit the form data to a file, Gif demo shown below:


Checkbox pass data to JSP program

Checkbox checkbox can pass an even more data.

The following is a simple HTML code, and code is stored in test.htm file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>

<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="google" checked="checked" /> Google
<input type="checkbox" name="w3big"  /> 本教程
<input type="checkbox" name="taobao" checked="checked" /> 
                                                淘宝
<input type="submit" value="选择网站" />
</form>

</body>
</html>

The above code in the browser to access the following:

The following is main.jsp file code for processing data box:

<%@ 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>本教程(w3big.com)</title>
</head>
<body>
<h1>从复选框中读取数据</h1>
<ul>
<li><p><b>Google 是否选中:</b>
   <%= request.getParameter("google")%>
</p></li>
<li><p><b>本教程是否选中:</b>
   <%= request.getParameter("w3big")%>
</p></li>
<li><p><b>淘宝是否选中:</b>
   <%= request.getParameter("taobao")%>
</p></li>
</ul>
</body>
</html>

By visiting http: // localhost: 8080 / testjsp / test.html main.jsp submit the form data to a file, Gif demo shown below:


Read all form parameters

Below we will use the HttpServletRequest getParameterNames () to read all parameters of the form, the method can get the names of all the variables, the method returns an enumeration.

Once we have a Enumeration (enumeration), we can call hasMoreElements () method to determine whether there are elements, and the use of 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>本教程(w3big.com)</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 test.htm file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>

<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="google" checked="checked" /> Google
<input type="checkbox" name="w3big"  /> 本教程
<input type="checkbox" name="taobao" checked="checked" /> 
                                                淘宝
<input type="submit" value="选择网站" />
</form>

</body>
</html>

Now we submit data files through browser access test.htm output results are as follows:

By visiting http: // localhost: 8080 / testjsp / test.html main.jsp submit the form data to a file, Gif demo shown below:

You can try to use more than JSP code reading other objects, such as text boxes, radio buttons, drop-down boxes or the like other forms of data.