List of useful methods of ServletRequest and HttpServletRequest

ServletRequest Interface

Below methods work with the metadata properties carried by requests:

  1. String getCharacterEncoding()

  2. Int getContentLength()

  3. String getContentType()

  4. Enumeration getLocales()

  5. Locale getLocale()

  6. String getProtocol () E.g. HTTP/1.1

  7. String getScheme() E.g.: http, https, ftp etc.

  8. Boolean isSecure ()

  9. void setCharacterEncoding (String enc)

 

Below methods retrieve request parameter details:

  1. String[] getParameterValues (String name)

  2. String getParameter (String name)

  3. Enumeration setParameterNames()

  4. Map getParameterMap()

 

Below methods return the IP Address, name and port of the IP interface on which this request was received:

  1. String getLocalAddr()

  2. String getLocalName()

  3. Int getLocalPort()

 

Below methods provide the host name of server and port to which the request was sent:

  1. String getServerName()

  2. Int getServerPort()

 

Below methods provide info of client or last proxy that sent request

  1. String getRemoteAddr()

  2. String getRemotePort()

 

Reading Request Bodies: Only one of the below method can be called on a ServletRequest object to get an input Stream or Reader, or it will cause an exception: IllegalStateException

  1. ServletInputStream getInputStream()

  2. Reader getReader ()

Note: Both these may throw IO exception if an error occurs during construction of stream

 

Request Dispatching:

  1. RequestDispacther getRequestDispatcher (String path)

    • For including or forwarding to resource at specified path

    • Path relative to current servlet or path relative to content root (starting with /)

Note: ServletContext’s version only allow path relative to the context root

 

HttpServletRequest Interface

HTTP Request Headers: The idea of a request header is specific to HTTP protocol and hence request header related methods are part of HttpServletRequest:

  1. Enumeration getHeaders (String name)

  2. String getHeader (String name)

  3. Enumeration getHeaderNames ()

  4. Int getIntHeader (String name)

  5. Long getDateHeader (String name)

 

Each of these methods returns a specific part of the request URL:

  1. String getContextpath()

    1. Context part of Request URI

  2. String getmethod()

    1. The HTTP method

  3. String getPathInfo()

    1. URI which follow servlet path but preceeds query string

  4. String getPathTranslated()

    1. Path info as a real path

  5. String getQueryString()

    1. Null if no query string

  6. String getRequestURI()

    1. Complete request URL from end of host name excluding query string

  7. StringBuffer getRequestURL ()

    1. Reconstruct URL used by client

    2. Does not contain query string

  8. String getServletPath()

    1. Returns section of the request URI from the context of the application to the mapped path to the servlet in DD

 

Security- Related methods in request object are:

  1. String getAuthType()

    1. Name of authenticated mechanism used to authenticate current client.

  2. String getRemoteuser()

    1. Username of client

  3. Principal getuserPrincipal()

    1. Java.security.Principal which reprecent the current client

  4. Boolean isUserInRole (string role)

Note: First 3 methods return null if client is not authenticated

 

Cookies: Container retrieves all cookies sent by client in an HTTP request. Create an instance of javax.servlet.http.cookie for each cookie. We can get an array of all cookie instances as:

  • Cookie[] getCookies()

Note: Container automatically takes care of cookies having any session management info like the jsessionid.

 

Session Management: HttpSession objects are always retrieved from the HttpservletRequest object , as the HttpSession object is dependent on clients session identifier like the jsessionid in JavaEE, ehich is either stored in cookies or in requestURI, and both these are available only from the request. Session management related methods in HttpServletRequest are:

  1. HttpSession getSession (Boolean create)

    1. Current valid session object

    2. If no session and

      1. Create is true- a new session is created and returned

      2. Create is false – returns null

  2. HttpSession getSession()

    1. Same as getSession (true)

  3. String getRequestedSessionId()

    1. Session id supplied by client

    2. Null if no session is supplied by lient

    3. Even if application invalidated it or changed with a new one, returns the original client supplied session id.

  4. Boolean isRequestedSessionIdValid()

    1. If the session id found in the request is still alid

  5. Boolean isRequestedSessionId from Cookies

  6. Boolean isRequestedSessionIdFromURL()

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!