The request HTTP request

The request HTTP request

 

request: the request 
    action: Get browser sends over the data 
    part: 
        a request line of the request header request body 
    operation request line 
        format: 
            Request Mode Request Resource Protocol / version 
        common methods: the HttpServletRequest 
            master 
                String getMethod (): Get request method 
                String getRemoteAddr () : get ip address 
                String getContextPath (): Gets the project name (in java / Day10) 
 
            
            understand: 
                getRequestURI (): obtained from the project name to the contents of the previous parameters   / Day10 / REGIST 
                getRequestURL (): complete with the agreement of the acquisition of path HTTP: // localhost / a fastMacedonia Day10 / REGIST 
                String the getQueryString (): GET request all parameters username = tom & password = 123
                String getProtocol (): Gets the protocol and version 
                
        For example: request line 
            GET / Day10 / Row username & password = tom = 123 HTTP / 1.1?

 

Case: request header method

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 
  <servlet>
    <servlet-name>RowServlet</servlet-name>
    <servlet-class>com.hjh.servlet.request.RowServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RowServlet</servlet-name>
    <url-pattern>/row</url-pattern>
  </servlet-mapping>
  
</web-app>

 

 

package com.hjh.servlet.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求方式
        Method = String request.getMethod (); 
        System.out.println ( "request method:" + Method); 
        
        // Get request resource 
        String URI = Request.getRequestURI (); 
        String URL = . Request.getRequestURL () toString () ; 
        System.out.println ( "URI:" URI + + "" + "URL:" + URL); 
        
        // Get request parameter string 
        string Query = request.getQueryString (); 
        System.out.println ( "request parameters string: "+ Query); 
        
        // get the protocol version 
        string = protocol request.getProtocol (); 
        System.out.println ( " protocol version: "+protocol);
        
        System.out.println ( "--------------------------- ------------ The following is an important method ---------------- " ); 
        
        // Get request the IP 
        String addr = request.getRemoteAddr (); 
        System.out.println ( " request the IP: "+ addr); 
        
        // item name Get 
        String the contextPath = request.getContextPath (); 
        System.out.println ( "project name:" + the contextPath);         
    } 

    protected  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        the doGet (Request, Response); 
    } 
}

 

 

Start the project, the browser enter the url "http: // localhost:? 8080 / Servlet / row username = hjh & password = 12345", carriage return, eclipse the console output is:

Request mode: the GET 
URI: / the Servlet / Row URL: HTTP: // localhost: 8080 / the Servlet / Row 
request parameters string: username = hjh & password = 12345 
Protocol version: the HTTP /1.1 
----------- ---------------- the following is an important method ---------------------------- 
request IP : 0: 0: 0: 0: 0: 0: 0: 1 
project name: / Servlet

 

 

 

Operation request header 
        format: key / value (value may be a plurality of values) 
        common methods: 
            ★ getHeader String (String key): Get the specified value (a) by the key 
            
            to understand: 
                the Enumeration the getHeaders (String name): Gets the key specified by value (more) 
                Enumeration getHeaderNames (): Gets the names of all of the request header 
                int getIntHeader (String Key): Gets an int request header
                 Long getDateHeader (String Key): acquisition time of request headers 
        important request header: 
            the User - Agent : Firefox Chrome browser kernel MSIE 
            Referer: page from there to the security chain

Case: request header

 <servlet>
    <servlet-name>HeaderServlet</servlet-name>
    <servlet-class>com.hjh.servlet.request.HeaderServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HeaderServlet</servlet-name>
    <url-pattern>/header</url-pattern>
  </servlet-mapping>

 

 

package com.hjh.servlet.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeaderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取浏览器内核
        User_agent request.getHeader = String ( "the User-Agent" ); 
        System.out.println ( "browser kernel:" + user_agent); 
        
        // get Referer 
        String = Referer request.getHeader ( "Referer" );
         IF (Referer = = null ) { 
            System.out.println ( "entered directly in the address bar" ); 
        } the else  IF (referer.contains ( "localhost" )) { 
            System.out.println ( "native input" ); 
        } the else  IF (referer.contains ( "192.167.23.156" )) { 
            System.out.println ( "Others " Others " );
        }else {
            System.out.println("兄弟,盗链可耻");
        }        
    }

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

}

 

 

url   http://localhost:8080/Servlet/header

 

console

Browser kernel: Mozilla / 5.0 (Windows NT 6.1 ; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 58.0.3029.96 Safari / 537.36 
directly in the address bar of

 

Guess you like

Origin www.cnblogs.com/hejh/p/11004171.html