Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP client request


May 12, 2021 JSP


Table of contents


The JSP client requests

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

The following table lists some important elements of the browser-side header, which will be frequently seen in future network programming:

Information Describe
Accept Specify the type of MIME that a browser or other client can handle. Its value is usually image/png or image/jpeg
Accept-Charset Specifies the set of characters that the browser wants to use. For example, ISO-8859-1
Accept-Encoding Specify the type of encoding. Its value is usually gzip or compress
Accept-Language Specifying the client's preferred language, the servlet returns the result set in the current language first, if the servlet supports that language. For example, en, en-us, ru, etc
Authorization Identify different users when visiting password-protected web pages
Connection Indicates whether the client can handle HTTP persistent connections. P ersistent connections allow clients or browsers to get multiple files in a single request. Keep-Alive means that persistent connections are enabled
Content-Length Available only for POST requests, representing the number of bytes of POST data
Cookie Return cookies previously sent to the browser to the server
Host Indicates the host name and port number in the original URL
If-Modified-Since Indicates that the client will only need the page if it is modified on the specified date. The server sends 304 yards to the client, indicating that there are no updated resources
If-Unmodified-Since In contrast to If-Modifyed-Since, the operation will only succeed if the document has not been modified after the specified date
Referer Marks the URL of the referenced page. For example, if you are on page 1 and then click a link to page 2, the URL of page 1 will be included in the header of the browser request page 2
User-Agent Used to distinguish between requests sent by different browsers or clients and to return different content to different types of browsers

HttpServletRequest class

The request object is an example of the Javax.servlet.http://httpservletRequest class. Each time a client requests a page, the JSP engine produces a new object to represent the request.

The request object provides a range of methods for obtaining HTTP headers, including form data, cookies, HTTP methods, and so on.

Next, we'll introduce some of the methods commonly used in JSP programming to get HTTP headers. See the table below for details:

Serial number Method . . .
1 Cookie[] getCookies()

Returns an array of all cookies owned by the client

2 Enumeration getAttributeNames()

Returns a collection of all property names for the request object

3 Enumeration getHeaderNames()

Returns a collection of names for all HTTP headers

4 Enumeration getParameterNames()

Returns a collection of all parameters in the request

5 HttpSession getSession()

Returns the session object for the request, and if not, creates one

6 HttpSession getSession(boolean create)

Returns the session object for the request, and if there is no and the parameter create is true, a new session object is returned

7 Locale getLocale()

Returns the Locale object of the current page, which can be set in response

8 Object getAttribute(String name)

Returns a property value with the name name name, null if it does not exist.

9 ServletInputStream getInputStream()

Returns the requested input stream

10 String getAuthType()

Returns the name of the authentication scheme to protect the servlet, such as "BASIC" or "SSL" or null if the JSP does not have protection in place

11 String getCharacterEncoding()

Returns the character encoding set name of the request

12 String getContentType()

Returns the MIME type of the request body and, if unknown, null

13 String getContextPath()

Returns the context path indicated in the request URI

14 String getHeader(String name)

Returns the header specified by name

15 String getMethod()

Return http methods such as GET, POST, or PUT in this request

16 String getParameter(String name)

Returns the parameters specified by name in this request and null if they do not exist

17 String getPathInfo()

Return any additional paths associated with this request URL

18 String getProtocol()

Returns the protocol name and version used for this request

19 String getQueryString()

Returns the query string contained in this request URL

20 String getRemoteAddr()

Returns the client's IP address

21 String getRemoteHost()

Returns the full name of the client

22 String getRemoteUser()

Returns the user who the client has authenticated by logging on to, and null if the user is not authenticate

23 String getRequestURI()

Returns the URI of the request

24 String getRequestedSessionId()

Returns the session ID specified by request

25 String getServletPath()

Returns the requested servlet path

26 String[] getParameterValues(String name)

Returns all values of the parameters of the specified name, or null if it does not exist

27 boolean isSecure()

Returns whether request is using an encrypted channel, such as HTTPS

28 int getContentLength()

Returns the number of bytes contained in the request body, if unknown return -1

29 int getIntHeader(String name)

Returns the value of the request header with the specified name

30 int getServerPort()

Returns the 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 as enumeration.

After you get the Enumeration object, you traverse the Enumeration object in a standard way, using the HasMoreElements() method to determine when to stop, and the nextElement() method to get the name of each parameter.

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>HTTP Header Request Example</title>
</head>
<body>
<center>
<h2>HTTP Header Request Example</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>
</center>
</body>
</html>

Access main.jsp will get the following results:

HTTP Header Request Example

Header Name Header Value(s)
accept */*
accept-language en-us
user-agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; MS-RTC LM 8)
accept-encoding gzip, deflate
host localhost:8080
connection Keep-Alive
cache-control no-cache

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