2 Minutes To Code (β Version)

JSP - 2 minute tutorial

Introduction to 'request' implicit object

When the webserver receives an http request, it bundles everything (headers, parameters, cookies) that it receives from the http client in a request object and this object is implicitly available to all jsp pages.

request object is an instance of javax.servlet.http.HttpServletRequest.

The example below shows how to get the http parameter "username" from the request object.
	   Welcome <%= request.getParameter("username") %>
The above code will produce the webpage shown below. Notice the parameter "username" in the url.

The other commonly used methods of the request object are :
Method Description
String getParameter(String) Gets the parameter sent by http client
String[] getParameterValues(String) Same as above, if parameter has multiple values as in select dropdown list, gets them as an array
Enumeration getParameterNames() Gets names of all parameters
Map getParameterMap() Gets map of parameter names and values
String getHeader(String) Get header value
Enumeration getHeaders(String) Gets all the headers
Enumeration getHeaderNames() Gets all header names
HttpSession getSession() Gets
HttpSession getSession(boolean) Gets the session of the 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 getAttributeNames() Gets names of all attributes stored in the request scope
Cookie[] getCookies() Gets all cookies sent by http client