Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read request headers by Servlet

Shulou Source: shulou.com Published: 2022-06-02 09:29:16 11月13日 Update

This article will explain in detail how Servlet reads request headers. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Read request headers in Servlet

It is very convenient to read HTTP headers in Servlet, as long as you call the getHeader method of HttpServletRequest. If the specified header information is provided in the customer request, getHeader returns the corresponding string; otherwise, it returns null. Some header information is often used, and they have special access methods: the getCookies method returns the contents of the Cookie header, which is parsed and stored in the array of Cookie objects. See the discussion in the Cookie section below; getAuthType and getRemoteUser methods read part of the Authorization header respectively; getDateHeader and getIntHeader methods read the specified header and then return the date value or integer value.

In addition to Servlet reading the specified headers, you can also use getHeaderNames to get an Enumeration object with all the header names in the request.

* in addition to viewing the request header information, we can also get some information from the request main command line. The getMethod method returns the request method, which is usually GET or POST, but it can also be HEAD, PUT, or DELETE. The getRequestURI method returns URI (URI is the part of the URL from the host and port to before the form data). GetRequestProtocol returns the third part of the request command, usually "HTTP/1.0" or "HTTP/1.1".

Example: output all request headers

The following Servlet example outputs all received request headers and their values in tabular form. In addition, the Servlet outputs three parts of the main request command: request method, URI, and protocol / version.

Package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ShowRequestHeaders extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text/html"); PrintWriter out = response.getWriter (); String title = "Show all request headers" Out.println (ServletUtilities.headWithTitle (title) + "\ n" + "+ title +"\ n "+" Request Method: "+ request.getMethod () +"\ n "+" RequestURI: "+ request.getRequestURI () +"\ n "+" Request Protocol: "+ request.getProtocol () +" public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response) }} this is the end of the article on "how to read request headers for Servlet". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Method information content command article section output instance object more relevant good practical three host name character string customer form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia OPPO Reno Xiaomi MariaDB