Request other functions

Keywords: Tomcat encoding

Article directory

1, General way to get request parameters

The following methods can be used to get the request parameters in both get and post request modes

(1) String getParameter(String name): get the parameter value according to the parameter name    
(2) String[] getParameterValues(String name): get the array of parameter values according to the parameter name 
	Similar: get the value of the check box, hobby = XX & Hobby = game
 (3) Enumeration < string > getparameternames(): get parameter names of all requests
 (4) Map < string, string [] > getparametermap(): get the map set of all parameters
@WebServlet("/requestDemo")
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);
            }
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get get request parameters
/*
        //Because the Get request method is universal, if it is Get method, you can directly jump to Post for execution
        this.doPost(request,response);
    }
}
*Chinese code scrambling:
	*get mode: tomcat 8 has solved the problem of getting mode scrambling
	*post mode: random code
	*Solution: before obtaining the parameters, set the encoding request.setCharacterEncoding("utf-8") of the request;

@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println(username);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

Solve:

@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Set the encoding of the stream
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        System.out.println(username);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

2, Request forwarding

A way of resource jump inside the server

The 1. step:
	(1) Get the request forwarder object through the request object: RequestDispatcher getRequestDispatcher(String path)
	(2) Use the RequestDispatcher object to forward: forward(ServletRequest request, ServletResponse response) 
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("requestDemo Was interviewed...");
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/servletDemo");
        requestDispatcher.forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

2. characteristics:
	(1) Browser address bar path does not change
	(2) It can only be forwarded to the internal resources of the current server, just as servletDemo and requestDemo are in the same Tomcat server
	(3) Forwarding is a one-time request, which can be found by the packet capturing tool and only forwarded once

3, 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
 * methods:
	(1) void setAttribute(String name,Object obj): store data
	(2) Object getattribute (string name): get value through key
	(3) Void removeaattribute (string name): remove key value pair through key
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("requestDemo Was interviewed...");

//        Store data in the request domain
        request.setAttribute("msg", "hello");
        //Forwarding
        request.getRequestDispatcher("/servletDemo").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

4, Get ServletContext

ServletContext getServletContext()

@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = request.getServletContext();
        System.out.println(servletContext);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

667 original articles published, 292 praised, 270000 visitors+
His message board follow

Posted by Jennie on Fri, 13 Mar 2020 01:10:35 -0700