Latest web development tutorials

JSP client requests

When a browser requests a page, it will send a series of information can not be read directly to the web server, because this information as part of the HTTP header information to be transmitted. You can check the HTTP protocol to get more information.

The following table lists some of the important contents of the browser header, after network programming you will often see this information:

information description
Accept Specifies the MIME type of browser or other client can handle. Its value is usually image / png or image / jpeg
Accept-Charset Specifies the character set to use the browser. For example, ISO-8859-1
Accept-Encoding Specify the encoding type. Its value is usually gzip or compress
Accept-Language Specify the client's preferred language, servlet will give priority to return a result set to the current language of the constitution, if the servlet supports this language words. For example en, en-us, ru etc.
Authorization Identify different users access to password-protected Web page
Connection It indicates whether the client can handle HTTP persistent connection. Persistent connection allows the client or browser fetches multiple files in a single request. Keep-Alive indicates enable persistent connections
Content-Length Only for POST requests, the number of bytes of data POST
Cookie Returns previously sent to the browser's cookies to the server
Host Pointed out that the original URL of the host name and port number
If-Modified-Since Show only when the page was modified on the specified date client only needs this website. The server sends 304 yards to the client that there is no updated resource
If-Unmodified-Since And If-Modified-Since the contrary, only the document has not been modified after the specified date, the operation will be successful
Referer URL of the page marked by reference. For example, if you are in page 1, then a link to the page 2, then page URL 1 will be included in the browser requests a page header 2
User-Agent It used to distinguish between different browsers or request sent by the client, and the different types of browser to return different content

HttpServletRequest class

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

request object provides a series of methods to get the HTTP header information, including form data, cookies, HTTP methods, and so on.

The next will introduce some common Get HTTP header in JSP programming methods. For details, please see table below:

No. Method & description
1 Cookie [] getCookies ()

Returns an array of all of the client's Cookie
2 Enumeration getAttributeNames ()

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

Returns a collection of all the names of the HTTP header
4 Enumeration getParameterNames ()

Returns a collection of all the parameters of the request
5 HttpSession getSession ()

Returns request the corresponding session object, if not, create a
6 HttpSession getSession (boolean create)

Returns request the corresponding session object if no parameters and create is true, returns a new session object
7 Locale getLocale ()

Returns this page Locale object may be provided in the response
8 Object getAttribute (String name)

Returns the name of the attribute value name, and if there is no return null.
9 ServletInputStream getInputStream ()

Returns an input stream request
10 String getAuthType ()

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

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

Returns the MIME type of the main request, or null if not known
13 String getContextPath ()

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

Returns the name specified in the message header
15 String getMethod ()

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

Returns the request parameter name specified, or null if not exists
17 String getPathInfo ()

Returns any extra path URL associated with this request
18 String getProtocol ()

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

This returns the query string request URL contains
20 String getRemoteAddr ()

Returns the IP address of the client
twenty one String getRemoteHost ()

Return the full name of the client
twenty two String getRemoteUser ()

Returned to the client through the user login authentication, if the user does not authenticate or null
twenty three String getRequestURI ()

Returns the request URI
twenty four String getRequestedSessionId ()

Returns the specified session ID request
25 String getServletPath ()

Back servlet path requested
26 String [] getParameterValues (String name)

Returns parameter specifies the name of all the values, or null if not exists
27 boolean isSecure ()

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

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

The returned value specifies the name of the request header
30 int getServerPort ()

Returns server port number

HTTP header examples

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

After obtaining the Enumeration object, the standard way to traverse the Enumeration object, hasMoreElements () method to determine when to stop using nextElement () method to get 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>本教程(w3big.com)</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, will get the following results:

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