Whenever a client makes an http request, it is received by the servlet container in http protocol semantics; the servlet then creates an HttpServletRequest object, stuffs it with request parameters, headers, cookies etc that is sent by the client and passes it to the servelt instance for further processing.
How to get parameters sent by http client from HttpServletRequest
If the parameters has multiple values (like in the case of a multi-valued select dropdown of a form element), then you can use the method String[] getParameterValues(String).
To get the names of all parameters, you can use the method Enumeration<String> getParameterNames().
How to get headers sent by http client from HttpServletRequest
To get a multi-valued header, use the method Enumeration<String> getHeaders(String).
To get the names of all headers, use the method Enumeration<String> getHeaderNames().
How to get session of http client from HttpServletRequest
If you need to get a session if it already exists and not create one , then use the method HttpSesison getSession(boolean) with argument as false as shown in the example below.
Other objects/services provided by HttpServletRequest object
Method
|
Description
|
Cookie[]
getCookies()
|
=>Gets all
cookies sent by http client
|
String
getContentType()
|
=>Gets the
content type sent by http client/browser in header
|
int
getContentLength()
|
=>Gets the
length of request body sent by http client in header
|
String
getCharacterEncoding()
|
=>Gets the
request's encoding sent by http client in header
|
String
getContextPath()
|
=>Gets the
webapp's name that the http url points to
|
String
getMethod()
|
=>Gets the
http method of the request made by client
|
String
getServletPath()
|
=>Returns the
part of this request's URL that calls the servlet
|
String
getQueryString()
|
=>Gets the
parameters contained in the request URL after the path
|
String
getRequestURL()
|
=>Gets the
complete url of http request
|
String
getRemoteAddr()
|
=>Gets the ip
of http client
|
String
getRemoteHost()
|
=>Gets the
hostname of http client
|
int
getRemotePort()
|
=>Gets the
tcp port of the http client machine used in the request
|
String
getLocalAddr()
|
=>Gets
local(webserver's) ip address
|
String
getServerName()
|
=>Gets the
local(webserver machine's) hostname
|
int
getLocalPort()
|
=>Gets the
tcp port actually involved in http request, different from getServerPort()
|
int
getServerPort()
|
=>Gets the
port on which webserver listens for incoming http request
|
Object
getAttribute(String)
|
=>Gets the
attribute that jsp developer stores in request object
|
setAttribute(String,
Object)
|
=>Sets an
object as an attribute in request object
|
removeAttribute(String)
|
=>Removes
attribute from request object.
|
Enumeration<String>
getAttributeNames()
|
=>Gets names
of all attributes stored in the request scope
|
0 comments:
Post a Comment