servlet

servlet life cycle:

1. Instantiation: When the servlet is called for the first time, it will be instantiated, and only once, so the servlet is a single instance

2. Initialization: After the construction method, the servlet inherits HttpServlet, init(ServletConfig) is the method of the parent class, this method is executed once

3. Provide service: service judges whether it is Post or Get

4. Destruction: two cases

              1. Restart the web application where the servlet is located

               2. The destroy() method will be called when tomcat is closed, but this usually happens quickly and is not easy to be found

5. Recycling: GC recycling


Get the submitted parameters: request.getParameter("parameter label") 

Return response: write back html 

String html = null;
html = "<div style='color:green'>something to  show</div>"
PrintWriter pw = response.getWriter ();
pw.println (html);

Which submissions belong to POST: 1. When method="post" is displayed on the form

                                 2. When ajax specifies the post method

Which submissions belong to GET:   1. The default submission method of form

                                2. If an address is accessed through a hyperlink

                                3. If you directly enter a place in the address bar

                                4. Ajax specifies when the get method is used


service(HttpServletRequest , HttpServletResponse ) method : Before entering doGet() or doPost(), service() will be executed first. It is judged by the service() method whether to call doGet() or doPost(). Therefore, sometimes the service() method is directly rewritten, and the corresponding service is provided in it, so there is no need to distinguish whether it is get or post.


For Chinese processing:

 1. Set the encoding format of submitted Chinese in html <meta charset="UTF-8">

 2. Before the servlet accepts the Chinese data, set the Chinese encoding method consistent with the submitted data: request.setCharacterEncoding("UTF-8")

 3. Before returning the Chinese response, set the returned content and the Chinese encoding format of the browser's existing content: response.setContentType("text/html; charset=UTF-8") , response.setCharacterEncoding("UTF-8") only set the return Chinese encoding format of the content

                                

 

Server jump and client jump

Server jump: The server automatically jumps to the interface. The client does not know that the jump has occurred. You can find that the address in the browser address bar has not changed, but it is no longer where you actually visit. The client only requests it once. request.getRequestDispatcher("URL").forward(request, response);

Client redirection: The server sends the address to be redirected to the client, and the client requests it again. The browser address bar becomes a new address, and the client requests twice. response.sendRedirect("URL");


Request to get parameters: 1.request.getParameter(): is a common method used to obtain single-valued parameters
                              2.request.getParameterValues(): used to obtain parameters with multiple values, such as hobbies submitted during registration, you can use Multiple choice.

                              3.request.getParameterMap(): Used to traverse all parameters and return the Map type.

<!DOCTYPE html>
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
<form action="register" method="get">
    名字 : <input type="text" name="name"> <br>
    Hobit : LOL<input type="checkbox" name="hobits" value="lol">
        DOTA<input type="checkbox" name="hobits" value="dota"> <br>
       
         <input type="submit" value="done">
</form>

servlet

protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        System.out.println("Get single-value parameter name:" + request.getParameter("name"));
 
        String[] hobits = request.getParameterValues("hobits");
        System.out.println("Get parameter hobits with multiple values: " + Arrays.asList(hobits));
 
        System.out.println("traverse all parameters through getParameterMap: ");
        Map<String, String[]> parameters = request.getParameterMap();
 
        Set<String> paramNames = parameters.keySet();
        for (String param : paramNames) {
            String[] value = parameters.get(param);
            System.out.println(param + ":" + Arrays.asList(value));
        }
 
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324787578&siteId=291194637