Get the general method of request parameters | |; Chinese scrambling problem | |; request forwarding

Keywords: Java Tomcat encoding

1. General way to get request parameters: the following methods can be used to get request parameters in both get and post request modes

1. String getParameter(String name): get the parameter value according to the parameter name. Username = ZS & password = 123
2. String[] getParameterValues(String name): get the array of parameter values according to the parameter name. Hobby = XX & Hobby = game
3. Enumeration < string > getparameternames(): get the parameter names of all requests
4. Map < string, string [] > getparametermap(): get the map set of all parameters

regist2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Registration page</title>
</head>
<body>

    <form action="/day14/requestDemo6" method="post">
        <input type="text" placeholder="enter one user name" name="username"><br>
        <input type="text" placeholder="Please input a password" name="password"><br>

        <input type="checkbox" name="hobby" value="game">Game
        <input type="checkbox" name="hobby" value="study">Study
        <br>

        <input type="submit" value="register">

    </form>

</body>
</html>

 RequestDemo6.java

package cn.itcast.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post get request parameters

        //Get parameter value according to parameter name
      /*  String username = request.getParameter("username");
        System.out.println(username);*/

       /*//Get an array of parameter values according to the parameter name
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }*/

        //Get parameter names for all requests

        /*Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println(name);
            String value = request.getParameter(name);
            System.out.println(value);
            System.out.println("----------------");
        }*/

        // Get the map set of all parameters
        Map<String, String[]> parameterMap = request.getParameterMap();
        //ergodic
        Set<String> keyset = parameterMap.keySet();
        for (String name : keyset) {

            //Get key get value
            String[] values = parameterMap.get(name);
            System.out.println(name);
            for (String value : values) {
                System.out.println(value);
            }

            System.out.println("-----------------");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get get request parameters
        //Get parameter value according to parameter name
       /* String username = request.getParameter("username");
        System.out.println("get");
        System.out.println(username);*/

        this.doPost(request,response);
    }
}

   

Chinese code scrambling:

* get method: tomcat 8 has solved the problem of getting method scrambling
* post mode: random code
* solution: before obtaining parameters, set the encoding request.setCharacterEncoding("utf-8") of request;

2. Request forwarding: a way of resource jump within the server


1. Steps:
1. Get the request forwarder object through the request object: RequestDispatcher getRequestDispatcher(String path)
2. Use RequestDispatcher object to forward: forward(ServletRequest request, ServletResponse response)

2. Features:
1. The browser address bar path does not change
2. It can only be forwarded to the internal resources of the current server.
3. Forwarding is a request

Shared data:


* domain object: a scoped object that can share data within the scope
* request domain: represents the scope of a request, which is generally used to share data among multiple resources for request forwarding
* method:
1. void setAttribute(String name,Object obj): store data
2. Object getattribute (string name): get value by key
3. Void removeaattribute (string name): remove key value pair through key

  

Get ServletContext:
            ServletContext getServletContext()

306 original articles published, 94 praised, 60000 visitors+
Private letter follow

Posted by shylock on Thu, 27 Feb 2020 02:04:50 -0800