Servlet HttpServletResponse objects, HttpServletRequest objects

 

HttpServletResponse object (response) of the conventional method

  • setCharacterEncoding ( "utf-8") // set the response of the coded character set
  • setContentType ( "text / html; charset = utf-8") // set the content type of the response, the coded character set

 

  • getWriter () // Get the character output stream, PrintWriter, not output binary content. Suitable for printing text response, such as html files.
  • getOutputStream () // Get an output stream of bytes, the ServletOutputStream, either the output text, and binary data can be output.

 

  • setHeader ( "refresh", "3") // regularly updated page (current page) requested

  • setHeader ( "refresh", "3; url = http: //www.baidu.com") // Timing jump. Automatically jump to Baidu after 3s. Between the number of seconds, separated by semicolons url default default url for the current page, the current page that is regularly updated.

 

  • sendRedirct ( "/ servlet2") // redirect. Tells the browser to request specifies utl, the browser will automatically send a request to the url. Total browser sends an HTTP request twice.

 

 

 

HttpServletRequest object (request) method commonly used

  • String getRemoteAddr () // get the ip address of the client
  • String getLocalAddr () // get server's IP address
  • String getServerName () // get server's domain address

 

  • String getMethod () // get request method, such as get, post
  • String getQueryString () // get the request string, that is, the address bar? The entire contents of the latter (including &, excluding?). This method can only get the parameters passed get way, can not be used post.
  • String getHeader (String name) // Get the value of a specified field in the request header

 

  • setCharactorEncoding ( "utf-8"); // set coded character set of request parameters

 

 

  // Get the request parameters (address bar? The latter part, form data)

  • String getParameter (String name) // Gets the value of the specified parameters (form). If no parameters, return null; if the parameter, but this parameter has no value, return an empty string; if there are a plurality of the parameters, only the first return value
  • String [] getParameter (String name) // If there are a plurality of the parameters, returns an array of strings. Commonly used to get the value of a check box.
  • Enumeration getParameterNames () // return a Enumeration object that contains all the parameter names
  • Map getParameterMap () // return a name containing all the parameters, Map object values

 

 

        // RequestDispatcher dispatcher means dispatching, distribution, dispatcher

  •  RequestDispatcher  rd = request.getRequestDispathcer(String path)   //获取RequestDispatcher对象
  • rd.forward (request, response) // request is forwarded. Servlet not currently processing the request url by another processing request, and returns a response. Conduct internal server, the client always sends only once HTTP request.
  • rd.include (request, response) // request contains the url Another outcome of treatment include it (replace sentence code), and this composition together with the processing result Servlet response back to the client.

 

 

  // When RequestDispatcher forward request, included, can be transmitted using the attribute data of other

  • request.setAttribute (String name, Object value) // set, modify
  • Object  request.getAttribute(String name)  //取出
  • Enumeration getAttributeNames () // Enumeration object containing all acquired a name for traversal attribute
  • request.removeAttribute (String name) // deleted, removed

 some additional attribute is the additional data in the request, attribute data form is not transmitted (parameter).

 attribute, parameter set 2 is separate methods.

 

 

 

Hotlink Protection

1  // Get referer header field value of 
2          String request.getHeader referer = ( "referer" );
 . 3          // request for obtaining the address of the site 
. 4          String sitePart = "HTTP: //" + to request.getServerName ();
 . 5          
. 6          // not Daolian 
. 7          IF (Referer =! null && referer.startsWith (sitePart)) {
 . 8              // go to the requested page 
. 9              the requestDispatcher requestDispatcher = request.getRequestDispatcher ( "/ download.html" );
 10              RequestDispatcher.forward (request , Response);
 . 11          }
 12 is          the else {
13              @ ...... Daolian 
14          }

 

 referer header field refers to the source of the HTTP request. Not the client's address.

For example, there is a page on a site http://www.example.com/index.html http://www.example.com/, there is a <a> links on this page: http: //www.example.com /articles/1.html,

<a> someone clicks this link, his browser will launch a HTTP request, referer header field of the request is http://www.example.com/index.html. This <a> clicked links from that page, referer header field url is the source of the page.

 

Referer header field can be used to detect the source of visitors (from which page visitor is through | to the website).

 

Visitors To access the resources on this website (watch videos, download resources, etc.), his browser makes an HTTP request, if the visitor clicks on a link provided on the resources page of this site, the referrer site, through;

If a visitor clicks on the page provides a link to the site is not (click on a link provided on the other site pages), it is hotlinking.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11401181.html