Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Form Processing

JSP Form Processing

When browsing the web, we often need to submit information to the server and have it processed by a background program. Browsers use the GET and POST methods to submit data to the server.


GET Method

The GET method appends the encoded request information to the URL, separated from the URL by a "?". As follows:

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

The GET method is the browser's default way of passing parameters; it is recommended not to use GET for sensitive information such as passwords.

When using get, there is a limit on the size of transmitted data (note that it is not a limit on the number of parameters), with a maximum of 1024 bytes.


POST Method

Some sensitive information, such as passwords, can be passed via the POST method; POST submits data implicitly.

POST-submitted data is invisible, while GET passes data in the URL (you can look at your browser's address bar).

JSP uses getParameter() to obtain passed parameters, and the getInputStream() method to handle requests with binary data streams from the client.


JSP Reading Form Data


Example of Using the GET Method with a URL

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

http://localhost:8080/testjsp/main.jsp?name=Runoob Tutorial&url=http://ww.runoob.com

testjsp is the project address.

The following JSP program in the main.jsp file is used to process form data submitted by the client; we use the getParameter() method to get the submitted data:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*" %>









Runoob Tutorial(runoob.com)





使用 GET 方法读取数据

  • 站点名: <%= request.getParameter("name")%>

  • 网址: <%= request.getParameter("url")%>

Next, we access via the browser http://localhost:8080/testjsp/main.jsp?name=Runoob Tutorial&url=http://ww.runoob.com The output is shown below:


Example of Using the GET Method with a Form

The following is a simple HTML form that submits client data via the GET method to main.jsp file:











Runoob Tutorial(runoob.com)







站点名:
网址:

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).

By accessing http://localhost:8080/testjsp/test.html to submit form data to the main.jsp file, the demonstration GIF is as follows:

Fill in the "Site Name" and "URL" forms and click the "Submit" button; it will output the result.


Example of Using the POST Method with a Form

Next, let's use the POST method to pass form data. Modify the code in the main.jsp and Hello.htm files as follows:

The code of the main.jsp file:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*" %>









Runoob Tutorial(runoob.com)





使用 POST 方法读取数据

  • 站点名: <% // 解决中文乱码的问题 String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8"); %> <%=name%>

  • 网址: <%= request.getParameter("url")%>

In the code we use **new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8")** to convert the encoding and prevent Chinese characters from becoming garbled.

The modified code for test.htm is as follows:











Runoob Tutorial(runoob.com)







站点名:
网址:

By accessing http://localhost:8080/testjsp/test.html to submit form data to the main.jsp file, the demonstration GIF is as follows:


Passing Checkbox Data to a JSP Program

A checkbox can pass one or even multiple values.

The following is simple HTML code, saved in the test.htm file:











Runoob Tutorial(runoob.com)







Google Runoob Tutorial 淘宝

Accessing the above code in a browser looks like this:

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



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*" %>









Runoob Tutorial(runoob.com)





从复选框中读取数据

  • Google 是否选中: <%= request.getParameter("google")%>

  • Runoob Tutorial是否选中: <%= request.getParameter("runoob")%>

  • 淘宝是否选中: <%= request.getParameter("taobao")%>

By accessing http://localhost:8080/testjsp/test.html to submit form data to the main.jsp file, the demonstration GIF is as follows:


Reading All Form Parameters

Below we will use HttpServletRequest 's getParameterNames() to read all form parameters. This method can obtain the names of all variables; it returns an enumeration.

Once we have an Enumeration object, we can call the hasMoreElements() method to determine whether there are 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.*" %>









Runoob Tutorial(runoob.com)





读取所有表单参数

<% Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("\n"); String paramValue = request.getParameter(paramName); out.println("\n"); } %>
参数名参数值
" + paramName + " " + paramValue + "

The content of the test.htm file is as follows:











Runoob Tutorial(runoob.com)







Google Runoob Tutorial 淘宝

Now we access the test.htm file via the browser to submit data, and the output is as follows:

By accessing http://localhost:8080/testjsp/test.html to submit form data to the main.jsp file, the demonstration GIF is as follows:

You can try using the above JSP code to read other objects, such as text boxes, radio buttons, or drop-down lists, and other forms of data.