๐Ÿ’ป Programming/JSP

[JSP] Request Object ( Client Request )

์‚ฌ์šฉ์ž๊ฐ€ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์›นํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๋ฉด ์›น๋ธŒ๋ผ์šฐ์ €๋Š” ์›น์„œ๋ฒ„๋กœ ๋งŽ์€ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ์ •๋ณด๋“ค์€ ๋ชจ๋‘ HTTP request ์˜ ํ—ค๋” ๋‚ด์— ํฌํ•จ๋˜์–ด ์ „๋‹ฌ๋œ๋‹ค. HTTP Protocol ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

 

์•„๋ž˜ ๋‚ด์šฉ์€ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์›น์„œ๋ฒ„์ชฝ์œผ๋กœ ๋ณด๋‚ด๋Š” ์ •๋ณด ์ค‘์— ์ค‘์š”ํ•œ ํ—ค๋” ์ •๋ณด๋ฅผ ๊ฐ„์ถ”๋ฆฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

HeaderDescription
AcceptThis 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-CharsetThis header specifies the character sets the browser can use to display the information. For example ISO-8859-1.
Accept-EncodingThis 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-LanguageThis 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.
AuthorizationThis header is used by clients to identify themselves when accessing password-protected Web pages.
ConnectionThis 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 of Keep-Alive means that persistent connections should be used
Content-LengthThis header is applicable only to POST requests and gives the size of the POST data in bytes.
CookieThis header returns cookies to servers that previously sent them to the browser.
HostThis header specifies the host and port as given in the original URL.
If-Modified-SinceThis 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-SinceThis 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.
RefererThis 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-AgentThis header identifies the browser or other client making the request and can be used to return different content to different types of browsers.



HttpServletRequest 

 HttpServletRequest ๊ฐ์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

S.N.Method & Description
1Cookie[] getCookies()
Returns an array containing all of the Cookie objects the client sent with this request.
2Enumeration getAttributeNames()
Returns an Enumeration containing the names of the attributes available to this request.
3Enumeration getHeaderNames()
Returns an enumeration of all the header names this request contains.
4Enumeration getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request.
5HttpSession getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
6HttpSession 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.
7Locale getLocale()
Returns the preferred Locale that the client will accept content in, based on the Accept-Language header
8Object getAttribute(String name)
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
9ServletInputStream getInputStream()
Retrieves the body of the request as binary data using a ServletInputStream.
10String 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
11String getCharacterEncoding()
Returns the name of the character encoding used in the body of this request.
12String getContentType()
Returns the MIME type of the body of the request, or null if the type is not known.
13String getContextPath()
Returns the portion of the request URI that indicates the context of the request.
14String getHeader(String name)
Returns the value of the specified request header as a String.
15String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
16String getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist.
17String getPathInfo()
Returns any extra path information associated with the URL the client sent when it made this request.
18String getProtocol()
Returns the name and version of the protocol the request.
19String getQueryString()
Returns the query string that is contained in the request URL after the path.
20String getRemoteAddr()
Returns the Internet Protocol (IP) address of the client that sent the request.
21String getRemoteHost()
Returns the fully qualified name of the client that sent the request.
22String getRemoteUser()
Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
23String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
24String getRequestedSessionId()
Returns the session ID specified by the client.
25String getServletPath()
Returns the part of this request's URL that calls the JSP.
26String[] getParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
27boolean isSecure()
Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
28int getContentLength()
Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.
29int getIntHeader(String name)
Returns the value of the specified request header as an int.
30int getServerPort()
Returns the port number on which this request was received.


HTTP Header Request ์˜ˆ์ œ

์•„๋ž˜๋Š” HttpServletRequest์˜ getHeaderNames()๋ฉ”์†Œ๋“œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ํ˜„์žฌ ์š”์ฒญ์— ๋Œ€ํ•œ HTTP header ์ •๋ณด๋ฅผ ์ฝ์–ด์™€์„œ Enumeration์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Enumeration์„ ์–ป์–ด์˜ค๋ฉด ์šฐ๋ฆฌ๋Š” hasMoreElements() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ nextElement() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ—ค๋”๋ช…์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” ๊ทธ๋ ‡๊ฒŒ ์–ป์–ด์˜จ ํ—ค๋”๋ช…์„ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ํ—ค๋”๋ช…์— ๋Œ€ํ•œ ์‹ค์ œ ๊ฐ’๊นŒ์ง€ ์–ป์–ด์™€์„œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. 

<%@ 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>

์œ„ ์†Œ์Šค์ฝ”๋“œ๋ฅผ main.jsp ํŒŒ์ผ์— ๋„ฃ๊ณ  ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด๋ณด์„ธ์š”. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„๊ฒƒ์ž…๋‹ˆ๋‹ค.


HTTP Header Request Example

Header NameHeader Value(s)
accept*/*
accept-languageen-us
user-agentMozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; MS-RTC LM 8)
accept-encodinggzip, deflate
hostlocalhost:8080

connection

Keep-Alive
cache-control

no-cache

 

 

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_client_request.htm