Java foundation of HttpServletRequest request

   HttpServletRequest acquisition request is a request line, request headers and the request body; anti-theft chain may be provided by this method, to obtain an address. Bear in mind that the way to solve the garbage.

一、HttpServletRequest:

  Overview and operating procedures detailed in HttpServletResponse!

Second, get the HTTP request:

 Third, the acquisition request line:

1, the client requests manner:

getMethod () String type is obtained;

2, access to the requested resource:

getContextPath () to get a String web application name (project name)

the getQueryString () obtained parameter string submitted after the get url addresses;

 getRequestURI () Gets URI address of type String

 getRequestURL () Gets the URL type StringBuffer

request.getRemoteAddr () clients to gain access to the client IP address

 

void the doGet protected (the HttpServletRequest Request, the HttpServletResponse Response)
            throws ServletException, IOException {
        // Get request method
        String Method request.getMethod = ();
        System.out.println ( "request mode is:" + Method);
        // URI acquisition request
        = Request.getRequestURI the URI String ();
        System.out.println ( "to the URI" + the URI);
        // Get request the URL
        the StringBuffer = request.getRequestURL the URL ();
        System.out.println ( "as the URL:" + URL );
        // URI of the as / WEB / LineServlet
        // as the URL of: HTTP: // localhost: 8080 / WEB / LineServlet
        // get WEB project name
        String name = request.getContextPath ();
        System.out.println ( "WEB project name: "+ name);
        // Get the string get request URL
        String Query = request.getQueryString ();
        System.out.println ( "get request parameters are:" Query +);
        // ip address acquisition client
        String ip = request. getRemoteAddr ();
        System.out.println ( "IP address:" IP +);
    }

 

Fourth, the acquisition request header:

 getHeader(String name)

Role referer header: get access to the source, do anti-hotlinking

 

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取refere头
        String refere = request.getHeader("Referer");
        String content = null;
        if(refere.startsWith("http://localhost:8080")){
            content="真的离婚了!";       
        }else{
            content="你是小偷!";
        }
        //解决乱码
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(content);       
    }

 

 

 

Fifth, to obtain the request body:

1, solve the garbage post submission of: request.setCharacterEncoding ( "UTF-8");

2, the way to solve the garbage get submitted:

            parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

 

void the doGet protected (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
        // Chinese garbled solution (POST request)
// Request.setCharacterEncoding ( "UTF-. 8");
        // Get request parameter
        . @ 1 is a single value parameters
        String name = request.getParameter ( "username");
        // gET solve the garbage: the information must first get to decode the encoded with the original table, and then again later in the code table is encoded
        name = new String (name. the getBytes ( "the ISO-8859-1"), "UTF =. 8");
        String request.getParameter Sex = ( "Sex");
        System.out.println (name + "......" Sex +);
        / . / 2 of multiple values
        String [] = hobbys request.getParameterValues ( "Hobby");
        // iterate
        for (String S: hobbys) {
            the System.out.println(s);
        }
        // 3 acquires all request parameters the MAP
        the Map <String, String []> request.getParameterMap Map = ();
        // iterate
        the Set <String> map.keySet SET = ();
        for (String STR: SET) {
            String [] = value as map.get (STR);
            for (String V: value) {
                System.out.println (STR + "..." + V);
            }
        }       

 

 

Six, request additional features:

1, request is a domain object, but also has the following features:

  setAttribute(String name, Object o)

  getAttribute(String name)

  removeAttribute(String name)

2, request completion request forwarding:

  Get request forwarder ---- path forwarding address

RequestDispatcher getRequestDispatcher(String path)

  Forwarding by forwarding object

requestDispathcer.forward(ServletRequest request, ServletResponse response)

Seven, ServletContext domain and the domain of comparative life cycle Request:

1、ServletContext:

      Created: server startup

      Destruction: the server closes

      Domain scope: the entire web application

2、request:

      Create: Create a request to access

      Destruction: the end of the request in response to the destruction of

      Scope domains: a request

Eight, forwarding and redirection difference:

1, two redirection request, forwarding a request

2, address redirection address bar changes, the same forwarding address

3, can access external Web sites redirect forwards can only access internal resources

4, forwarding performance is superior to redirect

(When needed by request forwarding band data, by the first data to tape Servlet01 Servlet02! Need to redirect the address transition by)

 

public class Servlet01 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向request域中存值
        request.setAttribute("name", "张三" );
        //请求转发
        request.getRequestDispatcher("/Servlet02").forward(request, response);
   
       
    }

 

 

public class Servlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从request 域中取值
        String name =(String)request.getAttribute("name");
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("hello"+name);
    }

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159676.htm