Engineering Full Stack Apps with Java and JavaScript
Below methods work with the metadata properties carried by requests:
String getCharacterEncoding()
Int getContentLength()
String getContentType()
Enumeration getLocales()
Locale getLocale()
String getProtocol () E.g. HTTP/1.1
String getScheme() E.g.: http, https, ftp etc.
Boolean isSecure ()
void setCharacterEncoding (String enc)
Below methods retrieve request parameter details:
String[] getParameterValues (String name)
String getParameter (String name)
Enumeration setParameterNames()
Map getParameterMap()
Below methods return the IP Address, name and port of the IP interface on which this request was received:
String getLocalAddr()
String getLocalName()
Int getLocalPort()
Below methods provide the host name of server and port to which the request was sent:
String getServerName()
Int getServerPort()
Below methods provide info of client or last proxy that sent request
String getRemoteAddr()
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
ServletInputStream getInputStream()
Reader getReader ()
Note: Both these may throw IO exception if an error occurs during construction of stream
Request Dispatching:
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
HTTP Request Headers: The idea of a request header is specific to HTTP protocol and hence request header related methods are part of HttpServletRequest:
Enumeration getHeaders (String name)
String getHeader (String name)
Enumeration getHeaderNames ()
Int getIntHeader (String name)
Long getDateHeader (String name)
Each of these methods returns a specific part of the request URL:
String getContextpath()
Context part of Request URI
String getmethod()
The HTTP method
String getPathInfo()
URI which follow servlet path but preceeds query string
String getPathTranslated()
Path info as a real path
String getQueryString()
Null if no query string
String getRequestURI()
Complete request URL from end of host name excluding query string
StringBuffer getRequestURL ()
Reconstruct URL used by client
Does not contain query string
String getServletPath()
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:
String getAuthType()
Name of authenticated mechanism used to authenticate current client.
String getRemoteuser()
Username of client
Principal getuserPrincipal()
Java.security.Principal which reprecent the current client
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:
HttpSession getSession (Boolean create)
Current valid session object
If no session and
Create is true- a new session is created and returned
Create is false – returns null
HttpSession getSession()
Same as getSession (true)
String getRequestedSessionId()
Session id supplied by client
Null if no session is supplied by lient
Even if application invalidated it or changed with a new one, returns the original client supplied session id.
Boolean isRequestedSessionIdValid()
If the session id found in the request is still alid
Boolean isRequestedSessionId from Cookies
Boolean isRequestedSessionIdFromURL()