Servlet form data


Release date:2023-12-12 Update date:2023-12-12 Editor:admin View counts:85

Label:

Servlet form data

In many cases, you need to pass some information, from the browser to the Web server, and eventually to the daemon. Browsers can pass this informationto the Web server in two ways: the GET method and the POST method.

GET method

The GET method sends encoded user information to the page request. Between the page and the encoded information? Characters are separated as follows:

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

The GET method, which is the default way to pass information from the browser to the Web server, produces a long string that appears in the browser’s address bar. Do not use the GET method if you are passing passwords or other sensitive information to the server. The GET method has asize limit: there is a maximum of 1024 characters in the request string.

This information is used QUERY_STRING header pass, and can be passed through QUERY_STRING environment variable access, Servlet uses doGet() method to handle this type of request.

POST method

Another reliable way to pass information to a daemon is the POST method. ThePOST method packages the information in the same way as the GET method, butdoesn’t the POST method treat the information as a URL? The text string after the character is sent, but the information is sent as a separate message. Messages are sent to the daemon as standard output, which you can parse and use. Servlet usage doPost() method to handle this type of request.

Use Servlet to read form data

Servlet handles form data, which is automatically parsed using different methods depending on the situation:

  • getParameter() can call request.getParameter() method to get the value of the form parameter.

  • getParameterValues() if the parameter appears more than once, the methodis called and multiple values are returned, such as a check box

  • getParameterNames() call this method if you want to get a complete list of all the parameters in the current request.

An example of the GET method using URL

Here is a simple URL that uses the GET method to pass two values to the HelloForm program.

Http://localhost:8080/TomcatTest/HelloForm?name= Rookie course & url=www.runoob.com

The following is the processing of input by the Web browser HelloForm.java Servlet program. We will use the getParameter() method, you can easily access the passed information:

package com.runoob.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloForm
 */
@WebServlet("/HelloForm")
public class HelloForm extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "Using the GET method to read form data";
        // Processing Chinese
        String name =new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String docType = "<!DOCTYPE html> \n";
        out.println(docType +
            "<html>\n" +
            "<head><title>" + title + "</title></head>\n" +
            "<body bgcolor=\"#f0f0f0\">\n" +
            "<h1 align=\"center\">" + title + "</h1>\n" +
            "<ul>\n" +
            "  <li><b>site</b>:"
            + name + "\n" +
            "  <li><b>website</b>:"
            + request.getParameter("url") + "\n" +
            "</ul>\n" +
            "</body></html>");
    }

    // Method for handling POST method requests
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

And then we’re web.xml create the following entries in the file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>HelloForm</servlet-name>
    <servlet-class>com.runoob.test.HelloForm</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloForm</servlet-name>
    <url-pattern>/TomcatTest/HelloForm</url-pattern>
  </servlet-mapping>
</web-app>

Powered by TorCMS (https://github.com/bukun/TorCMS).