位置:首页 > Java技术 > JSP在线教程 > JSP客户端请求

JSP客户端请求

当浏览器请求的网页,它会发送大量的信息,而不能直接因为这些信息作为出行的HTTP请求报头的一部分被读取Web服务器。您可以查看HTTP协议进行更多这方面的信息。

以下是来自浏览器端,你会使用非常频繁的网络编程的重要头信息:

描述
Accept This header specifies the MIME types that the browser or other clients can handle. Values of image/png or image/jpeg are the two most common possibilities.
Accept-Charset This header specifies the character sets the browser can use to display the information. For example ISO-8859-1.
Accept-Encoding This header specifies the types of encodings that the browser knows how to handle. Values of gzip or compress are the two most common possibilities.
Accept-Language This header specifies the client's preferred languages in case the servlet can produce results in more than one language. For example en, en-us, ru, etc.
Authorization This header is used by clients to identify themselves when accessing password-protected Web pages.
Connection This header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files with a single request. A value ofKeep-Alive means that persistent connections should be used
Content-Length This header is applicable only to POST requests and gives the size of the POST data in bytes.
Cookie This header returns cookies to servers that previously sent them to the browser.
Host This header specifies the host and port as given in the original URL.
If-Modified-Since This header indicates that the client wants the page only if it has been changed after the specified date. The server sends a code, 304 which means Not Modified header if no newer result is available.
If-Unmodified-Since This header is the reverse of If-Modified-Since; it specifies that the operation should succeed only if the document is older than the specified date.
Referer This header indicates the URL of the referring Web page. For example, if you are at Web page 1 and click on a link to Web page 2, the URL of Web page 1 is included in the Referer header when the browser requests Web page 2.
User-Agent This header identifies the browser or other client making the request and can be used to return different content to different types of browsers.

HttpServletRequest 对象:

request 象是javax.servlet.http.HttpServletRequest对象的一个实例。每次客户端请求一个页面的JSP引擎创建一个新的对象来表示该请求。

request 对象提供的方法来获得,包括表单数据的HTTP头信息,饼干,HTTP方法等。

有以下几个重要的方法,可以用来读取HTTP头在你的JSP程序。这些方法可使用HttpServletRequest对象代表客户端请求的Web服务器。

S.N. 方法 & 描述
1 Cookie[] getCookies()
Returns an array containing all of the Cookie objects the client sent with this request.
2 Enumeration getAttributeNames()
Returns an Enumeration containing the names of the attributes available to this request.
3 Enumeration getHeaderNames()
Returns an enumeration of all the header names this request contains.
4 Enumeration getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request.
5 HttpSession getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
6 HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
7 Locale getLocale()
Returns the preferred Locale that the client will accept content in, based on the Accept-Language header
8 Object getAttribute(String name)
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
9 ServletInputStream getInputStream()
Retrieves the body of the request as binary data using a ServletInputStream.
10 String getAuthType()
Returns the name of the authentication scheme used to protect the servlet, for example, "BASIC" or "SSL," or null if the JSP was not protected
11 String getCharacterEncoding()
Returns the name of the character encoding used in the body of this request.
12 String getContentType()
Returns the MIME type of the body of the request, or null if the type is not known.
13 String getContextPath()
Returns the portion of the request URI that indicates the context of the request.
14 String getHeader(String name)
Returns the value of the specified request header as a String.
15 String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
16 String getParamet