Introduction to Servlet read request headers

Source: Internet
Author: User

Read Request headers in Servlet

It is very convenient for the Servlet to read the HTTP header. You only need to call the getHeader method of HttpServletRequest. If the specified header information is provided in the customer request, getHeader returns the corresponding string; otherwise, null is returned. Some header information is often used. They have a special access method: The getCookies method returns the content of the Cookie header, Which is parsed and stored in the array of Cookie objects, for more information, see the Cookie section. The getAuthType and getRemoteUser Methods read part of the Authorization header. The getDateHeader and getIntHeader Methods read the specified header and return the date value or integer value.

In addition to the Servlet reading the specified header, you can use getHeaderNames to obtain an Enumeration object of all header names in the request.

Finally, in addition to viewing the request header information, we can also obtain some information from the request's main command line. The getMethod method returns the request method. The request method is usually GET or POST, but may also be HEAD, PUT, or DELETE. The getRequestURI method returns the part of the URL from the host and port to the form data ). GetRequestProtocol returns the third part of the Request command, which is generally "HTTP/1.0" or "HTTP/1.1 ".

Instance: Output all request headers

The following Servlet instance outputs all received request headers and their values in a table. In addition, the Servlet outputs three parts of the main request command: Request Method, URI, protocol/version.

 
 
  1. Package hall;
  2.  
  3. Import java. io .*;
  4. Import javax. servlet .*;
  5. Import javax. servlet. http .*;
  6. Import java. util .*;
  7.  
  8. Public class ShowRequestHeaders extends HttpServlet {
  9. Public void doGet (HttpServletRequest request,
  10. HttpServletResponse response)
  11. Throws ServletException, IOException {
  12. Response. setContentType ("text/html ");
  13. PrintWriterOut=Response. GetWriter ();
  14. StringTitle="Show All request headers";
  15. Out. println (ServletUtilities. headWithTitle (title) +
  16. "\ N" +
  17. "
  18. "+ Title +"
  19. \ N "+
  20. "Request Method:" +
  21. Request. getMethod () +"
  22. \ N "+" Request URI: "+
  23. Request. getRequestURI () +"
  24. \ N "+" Request Protocol: "+
  25. Request. getProtocol () +"
  26. Public void doPost (HttpServletRequest request,
  27. HttpServletResponse response)
  28. Throws ServletException, IOException {
  29. DoGet (request, response );
  30. }
  31. }
  1. Introduction to functions of Servlet 2.4
  2. Introduction to multiple Servlet Interfaces
  3. Interface introduction-Servlet Context
  4. Servlet Registration Method
  5. Introduction to Servlet containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.