In this article, we will learn the related operations of HttpServletRequest on request header. In fact, there are two methods to learn. The first is to get the value of specific attribute in request header. The second method is to get all attributes. The return is an enumeration type.
1. Get the value of name in a request header
Here name is demonstrated by user agent. This value tells us what type of browser is requested. It can be seen clearly that it is ie, Firefox and chrome.
package com.anthony.servlet; 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 ServletDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Get the value of a name in the request header, such as user agent String browser = req.getHeader("User-Agent"); System.out.println(browser); if(browser.toLowerCase().contains("msie")) { System.out.println("You're using IE Browser."); }else if(browser.toLowerCase().contains("firefox")) { System.out.println("You're using Firefox."); }else if(browser.toLowerCase().contains("chrome")) { System.out.println("You're using Google browser."); }else { System.out.println("You're using a different browser."); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
Deploy to tomcat, I use Firefox to access, print the log as follows
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0 You're using Firefox.
2. Get the names of all request headers
First, extract the above code into method test1(), then write to get all names and traverse the enumeration object.
package com.anthony.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //test1(req); //Get names in all request headers Enumeration<String> headerNames = req.getHeaderNames(); while(headerNames.hasMoreElements()) { String e = (String)headerNames.nextElement(); System.out.println(e + ":" + req.getHeader(e)); } } private void test1(HttpServletRequest req) { //Get the value of a name in the request header, such as user agent String browser = req.getHeader("User-Agent"); System.out.println(browser); if(browser.toLowerCase().contains("msie")) { System.out.println("You're using IE Browser."); }else if(browser.toLowerCase().contains("firefox")) { System.out.println("You're using Firefox."); }else if(browser.toLowerCase().contains("chrome")) { System.out.println("You're using Google browser."); }else { System.out.println("You're using a different browser."); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
Deploy to tomcat server and test the effect
host:localhost:8080 user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0 accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language:en-US,zh-CN;q=0.7,en;q=0.3 accept-encoding:gzip, deflate cookie:jenkins-timestamper-offset=-28800000 connection:keep-alive upgrade-insecure-requests:1 cache-control:max-age=0
3.getHeaders(name)
If more than one name in a request header is the same, this method can be used. For example, we introduced that setHeader() can set multiple repeated encoding s equal to UTF-8, so we can use this method. This method is generally not useful.