What is headers in servlet
When a browser requests a web page, it generally sends some additional headers which specify things such as:. It's beyond the scope of this tutorial to give all the details of every possibly HTTP request header. And it's actually quite rare to need to read request headers. If necessary, it is recommended that you look at the HTTP protocol specification if you need more details.
However, we'll look at a couple of useful cases here by way of example. Usually, we'll know the name of the request header that we want to read. For example, here is how to read the user-agent header, which essentially tells us which browser IE, Mozilla, Safari etc and operating system is making the request:. Note that we should be careful of null values being returned for a given header if it wasn't sent by the client. Note that for a given header, knowing what the actual format of data that we might get can be something of a black art and the user-agent string is one of the worst offenders!
Although it is not so usual to need to read request headers, here are some possibly useful cases:. As mentioned above, the user-agent can give us the browser or robot and version, plus some information about plugins installed.
Please mail your requirement at [email protected] Duration: 1 week to 2 week. Servlet Tutorial. War File welcome-file-list Load on startup. Servlet Collaboration. RequestDispacher sendRedirect. Servlet Advance Session Tracking. Displaying all the header information in the servlet Syntax of getHeaderNames method Syntax of getHeader method Example of displaying all the header information in the servlet.
Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Possible values are gzip or x-gzip and compress or x-compress. Multiple encodings should be represented as a comma-separated list in the order in which the encodings were applied to the data.
Use setHeader to set the value of a header:. This method sets the value of the named header as a String. The name is case insensitive, as it is for all these methods. If the header had already been set, the new value overwrites the previous one. Headers of all types can be set with this method. If you need to specify a time stamp for a header, you can use setDateHeader :. This method sets the value of the named header to a particular date and time. The method accepts the date value as a long that represents the number of milliseconds since the epoch midnight, January 1, GMT.
If the header has already been set, the new value overwrites the previous one. Finally, you can use setIntHeader to specify an integer value for a header:. This method sets the value of the named header as an int. The containsHeader method provides a way to check if a header already exists:. This method returns true if the named header has already been set, false if not. In addition, the HTML 3. This technique does not provide any special benefit to servlets; it was developed for use with static documents, which do not have access to their own headers.
One of the useful things a servlet can do using status codes and headers is redirect a request. This is done by sending instructions for the client to use another URL in the response. Redirection is generally used when a document moves to send the client to the new location , for load balancing so one URL can distribute the load to several different machines , or for simple randomization choosing a destination at random.
Example shows a servlet that performs a random redirect, sending a client to a random site selected from its site list. Depending on the site list, a servlet like this could have many uses. As it stands now, it's just a jump-off point to a selection of cool servlet sites.
0コメント