Servlet (三)——(HttpServlet)Request

​ request is a parameter of the Servlet.service() method, and the type is javax.servlet.http.HttpServletRequest. When the client makes each request, the server will create a request object, and encapsulate the request data into the request, and then pass it to the service() method when calling the Servlet.service() method, which shows that in the service() method The request data can be obtained through the request object.

request line

request header

Blank line

Request body (get has no request body)

write picture description here

1. Obtain common information:

Related methods:
  • Get client IP, request.getRemoteAddr()

    For example, the commonly seen title is the IP block

  • The request method, request.getMethod(), may be POST or GET

2. Get the request header:

Related methods:
  • String getHeader(String name), for single-value headers
  • int getIntHeader(String Name), for request headers of single-value int type
  • long getDateHeader(String name), for single-value millisecond request headers
  • Enumeration getHeader(String name), for multi-value request headers
Case:
  • Identify user browser type through User-Agent

    String addr = request.getRemoteAddr();//获取客户端IP地址
    System.out.println("IP: "+ addr);
    System.out.println("请求方式: " + request.getMethod());//获取请求方式
    String userAgent = request.getHeader("User-Agent");//获取名为User-Agent的请求头
    System.out.println(userAgent);
  • Use the Referer request header to prevent anti-leech

    The Referer request header will store the address of the request source

    String referer =  request.getHeader("Referer");
    System.out.println(referer);//输出请求来源的地址
    //如果请求是从浏览器地址行输入的地址(referer值会为null)或者请求地址中没有"localhost"
    if(referer == null || !referer.contains("localhost")){
    response.sendRedirect("http://www.baidu.com");
    }else{
    System.out.println("hello!");
    }

3. Get the request URL:

Related methods:
  • String getScheme(): Returns the request protocol, for example: http
  • String getServerName(): returns the hostname, for example: localhost
  • int getServerPort(): returns the server port number, for example: 8080
  • String getContextPath(): returns the context path, for example: /hello
  • String getQueryString(): Returns the parameters in the request URL, for example: name=zhangSan
  • String getRequestURI(): Returns the request URI path, for example: /hello/oneServlet
  • String getRequestURI(): Returns the URL path, which is equal to the entire request path without project parameters.
  • String getServletPath(): returns the servlet path, for example: /oneServlet
    write picture description here

Fourth, get the request parameters:

1. Common client transfer parameters:
  • Direct input in the browser address bar: must be a GET request;
  • Hyperlink: must be a GET request;
  • Form: It can be GET or POST, depending on the value of the method attribute;

Take a look at FServlet.java this Demo! ! !

2. The difference between GET request and POST request:
  • GET request

    The request parameters will be displayed in the browser's address bar, so it is not safe;

    The length of the request parameter is limited to 1K;

    The GET request has no request body, and the parameter encoding cannot be set through request.setCharacterEncoding();

  • POST request

    The request parameters will not display the browser's address bar, which is relatively safe;

    There is no limit to the length of request parameters;

3. Related methods:
  • (Commonly used) String getParameter(String name): Get the parameter value by specifying the name;
  • String[] getParameterValues(String name): When multiple parameters have the same name, you can use the method to get them;
  • Enumeration getParameterNames(): Get the names of all parameters;
  • (Commonly used) Map getParameterMap(): Get all parameters and encapsulate them in Map, where key is the parameter name and value is the parameter value, because a parameter name may have multiple values, so the parameter value is String[], not String

5. Request forwarding and request inclusion

Whether it is request forwarding or request inclusion, it means that multiple servlets jointly process a request. For example, Servlet1 handles the request, and then Servlet1 forwards it to Servlet2 to continue processing the request.

//使用request获取RewuestDispatcher对象(调度器),方法的参数是被转发或被包含的servlet的servlet路径即MyServlet的<url-pattern>值
RewuestDispatcher rd = request.getRewuestDispatcher("/MyServlet");
1. Request forwarding:

rd.forward(request,response);

Call the forwarding method of the "scheduler", which is equivalent to telling the server to call the service() method of MyServlet.

System.out.println("AServlet");
RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");
rd.forward(request, response);

—>

System.out.println("MyServlet");
2. The request contains:

rd.include(request,response);

In AServlet, include the request to MyServlet.

System.out.println("AServlet");
RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");
rd.include(request, response);

—>

System.out.println("BServlet");
3. Compare:
  • If the request is forwarded to MyServlet in AServlet, the response body is not allowed to be output in AServlet , that is, response.getWriter() and response.getOutputStream() can no longer be used to output to the client, this work should be done by BServlet; If you use request inclusion, then there is no such restriction;
  • Although the request forwarding cannot output the response body, the response header can still be set, for example: response.setContentType("text/html;charset=utf-8");
  • The request contains mostly applied in JSP pages to complete the merging of multiple pages;
  • Request forwarding is mostly applied in servlets, and most of the forwarding targets are JSP pages;

write picture description here

6. Request forwarding and redirection:

  • Request forwarding is one request, while redirection is two requests;
  • After the request is forwarded, the browser address bar will not change, but the redirection will change, because the redirection is two requests;
  • The target of request forwarding can only be the resources in this application, and the target of redirection can be other applications;
  • Request forwarding has the same request method for AServlet and BServlet, that is, either GET or POST, because request forwarding is a request;
  • The second request redirected must be GET;,

Seven, request domain:

1. Three major domain objects in Servlet:
  • request
  • session
  • application
2. Methods (the three major domain objects in Servlet have these methods, but the scope is different):
  • void setAttribute(String name , Object value)
  • Object getAttribute(String name)
  • void removeAttribute(String name)
    write picture description here

Guess you like

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