JSP client request


When the browser requests a web page, it sends a series of information to the network server that cannot be read directly because the information is transmitted as part of the HTTP header. You can check out the HTTP protocol for more information.

The following table lists some important contents of the browser-side information header. You will often see this information in future network programming:

##                Accept               Specifies the MIME types that the browser or other client can handle. Its value is usually ##               Accept-Charset##             Accept-Encoding               Specify the encoding type. Its value is usually or             Specify the client's preferred language. The servlet will give priority to returning a result set in the current language, if the servlet supports this language. Such as en, en-us, ru, etc.             Identify different users when accessing password-protected web pages             Indicates whether the client can handle HTTP persistent connections. Persistent connections allow the client or browser to fetch multiple files in one request.Keep-Alive means enabling persistent connections##               Referer             Marks the URL of the referenced page. For example, if you are on page 1 and then click on a link to page 2, then the URL of page 1 will be included in the header of the browser request for page 2               User-Agent             Used to distinguish requests sent by different browsers or clients and return different content to different types of browsers

HttpServletRequest class

The request object is an instance of the javax.servlet.http.HttpServletRequest class. Whenever a client requests a page, the JSP engine generates a new object to represent the request.

The request object provides a series of methods to obtain HTTP information headers, including form data, cookies, HTTP methods, etc.

Next, we will introduce some methods commonly used in JSP programming to obtain HTTP information headers. Please see the table below for details:

InformationDescription
image/png or image/jpeg
            Specifies the character set to be used by the browser. For example, ISO-8859-1
gzipcompress##                 Accept-Language
            Authorization
            Connection
            Content-Length             Only applicable to POST requests, indicating the number of bytes of POST data
              Cookie             Return cookies previously sent to the browser to the server
            Host             Indicate the host name and port number in the original URL
            If-Modified-Since             Indicates that the client only needs this web page if it has been modified on the specified date. The server sends a 304 code to the client, indicating that there are no updated resources
              If-Unmodified-Since             Contrary to If-Modified-Since, the operation will succeed only if the document has not been modified after the specified date
Serial NumberMethod& describe
1Cookie[] getCookies()


Returns an array of all cookies on the client
            2Enumeration getAttributeNames()


Returns a collection of all attribute names of the request object
            3Enumeration getHeaderNames()


Returns the name set of all HTTP headers
            4Enumeration getParameterNames()


Returns the set of all parameters in the request
            5HttpSession getSession()


Return the session object corresponding to the request. If there is no session object, create one
              6HttpSession getSession(boolean create)


Returns the session object corresponding to the request. If there is none and the parameter create is true, a new session object is returned
                7Locale getLocale()


Returns the Locale object of the current page, which can be set in the response
            8Object getAttribute(String name)


Returns the attribute value named name, or null if it does not exist.
              9ServletInputStream getInputStream()


Return the requested input stream
            10String getAuthType()


Returns the name of the authentication scheme used to protect the servlet, such as "BASIC" or "SSL" or null if no protection measures are set for the JSP
                11String getCharacterEncoding()


Returns the character encoding set name of the request
            12String getContentType()


Returns the MIME type of the request body, or null if unknown
                13String getContextPath()


Returns the context path specified in the request URI
            14String getHeader(String name)


Return the information header specified by name
            15String getMethod()


Return the HTTP method in this request, such as GET, POST, or PUT
                16String getParameter(String name)


Returns the parameter specified by name in this request, if it does not exist, returns null
              17String getPathInfo()


Returns any additional paths associated with this request URL
            18String getProtocol()


Returns the protocol name and version used by this request
              19String getQueryString()


Returns the query string
contained in this request URL 20String getRemoteAddr()


Return the client's IP address
            21String getRemoteHost()


Returns the full name of the client
            22String getRemoteUser()


Returns the user who has passed login authentication on the client. If the user is not authenticated, returns null
              23String getRequestURI()


Return the URI of the request
            24String getRequestedSessionId()


Returns the session ID specified by request
            25String getServletPath()


Returns the requested servlet path
            26String[] getParameterValues(String name)


Returns all values ​​of the parameter with the specified name, or null if it does not exist
                27boolean isSecure()


Returns whether the request uses an encrypted channel, such as HTTPS
                28int getContentLength()


Returns the number of bytes contained in the request body. If unknown, returns -1
              29int getIntHeader(String name)


Returns the value of the request header with the specified name
            30int getServerPort()


Return server port number

HTTP header example

In this example, we will use the getHeaderNames() method of the HttpServletRequest class to read the HTTP header. This method returns the header information of the current HTTP request in the form of an enumeration.

After obtaining the Enumeration object, use the standard method to traverse the Enumeration object, use the hasMoreElements() method to determine when to stop, and use the nextElement() method to obtain the name of each parameter.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<h2>HTTP 头部请求实例</h2>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>Header Name</th><th>Header Value(s)</th>
</tr>
<%
   Enumeration headerNames = request.getHeaderNames();
   while(headerNames.hasMoreElements()) {
      String paramName = (String)headerNames.nextElement();
      out.print("<tr><td>" + paramName + "</td>\n");
      String paramValue = request.getHeader(paramName);
      out.println("<td> " + paramValue + "</td></tr>\n");
   }
%>
</table>
</body>
</html>

Access main.jsp, you will get the following results:

jspheadmsg.jpg

You can try other methods of the HttpServletRequest class in the above code.