Thursday, 19 April 2018

Introduction to HttpServletRequest

HttpServletRequest, http request,HttpServletRequest ,How to get parameters sent by http client from HttpServletRequest,getParameter(String),HttpServletRequest ,String[] getParameterValues(String),Enumeration getParameterNames(),How to get headers sent by http client from HttpServletRequest,HttpServletRequest ,String getHeader(String),Enumeration getHeaders(String),Enumeration getHeaderNames(),How to get session of http client from HttpServletRequest,HttpSession getSession(),HttpSesison getSession(boolean),Other objects/services provided by HttpServletRequest object,HttpServletRequest ,

Servlets are managed/executed by servlet containers like Apache Tomcat or Jetty

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

To get the parameters that the http client sends with the http request, you use the method String getParameter(String) of the HttpServletRequest object as shown in the example below.

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(). 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    String userName = req.getParameter("user");  
    String[] hobbies = req.getParameterValues("Interest");  
      
    Enumeration<String> paramNames = req.getParameterNames();  
    Map paramMap = req.getParameterMap();  
}  

How to get headers sent by http client from HttpServletRequest

The http clients sends headers along with the http request and you can get these from the HttpServletRequest by using the method String getHeader(String).

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()
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    String language = req.getHeader("lang");  
    Enumeration<String> acceptList = req.getHeaders("Accept");  
      
    Enumeration<String> headerNames = req.getHeaderNames();  
}  

How to get session of http client from HttpServletRequest

If you need to use sessions, you can create a session or get an existing session using the method HttpSession getSession() of the HttpServletRequest object.

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. 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    HttpSession session = req.getSession();  
          
    HttpSession existingSession = req.getSession(false);  
}

Other objects/services provided by HttpServletRequest object

The other commonly used methods of the HttpServletRequest object are : 
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










DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment