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()

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)