We will now concentrate on the basic aspect of using a Servlet which is requests and response. But we will currently concentrate only on ServletRequest and HttpServletRequest.

Points to Ponder: 

  • ServletRequest is not in any way responsible for fetching HTTP related properties.
  • ServletRequest and HttpServletRequest both are interfaces and you cannot directly use them.
  • The interface implementations depend on the vendor and not on the developer.

ServletRequest 

Lets look at the methods present in the ServletRequest Interface first and categorize them. There are in total 29 methods. For making the categories first we would have to look as to what are things to carry out when we receive a request. The things we would like to do are:

1.  Read the parameters obtained from the form input.

Enumeration  getParameterNames()
 
String  getParameter( String name )
 
String[]  getParameterValues( String name )
 
Map  getParameterMap()

2. For logging purpose, inquire who sent the request and through which protocol,etc.

String  getRemoteAddr()
 
String  getRemoteHost() 
 
String  getProtocol() 
 
String  getScheme() 
 
String  getRealPath( String path ) 
 
int  getRemotePort()

3. Perform a check on Attributes which might be lying around or maybe do a cleanup.

Enumeration  getAttributeNames()
 
void  setAttribute( String name, Object o ) 
 
Object  getAttribute( String name ) 
 
void  removeAttribute( String name )

4. Read some user preferences

Locale  getLocale()
 
Enumeration  getLocales() 
 
String  getCharacterEncoding() 
 
String  getContentType() 
 
int  getContentLength()

5. Inquire about where your own servlet. This is only required in for logging purposes as to which servlet might modify your business objects which can be then used while debugging.

String  getServerName()
 
int  getServerPort() 
 
int  getLocalPort() 
 
String  getLocalAddr() 
 
String  getLocalName()

6. Get some input sources if you want yourself parse throught the request information. Remember both these throw an IOException.

ServletInputStream  getInputStream() throws java.io.IOException
 
BufferedReader  getReader() throws java.io.IOException

7. Get a dispatcher for including or forwarding the request. This is a single method.

RequestDispatcher  getRequestDispatcher( String path )

8. Some other non important functions

boolean  isSecure()
 
void  setCharacterEncoding( String env ) throws java.io.UnsupportedEncodingException

HttpServletRequest 

Now lets take a look at the HttpServletRequest interface which implements the ServletRequest.

There are four fields

String  BASIC_AUTH
 
String  CLIENT_CERT_AUTH 
 
String  DIGEST_AUTH 
 
String  FORM_AUTH

1. Getting Header Information. Remember in this that, the getDateHeader(String name) returns a long rather than a java.util.Date

long  getDateHeader( String name )
 
String  getHeader( String name ) 
 
Enumeration  getHeaderNames() 
 
Enumeration  getHeaders( String name ) 
 
int  getIntHeader( String name )

2. You can request the HTTP specific information like URL etc. The main difference between the ServletRequest and HttpServletRequest interfaces is that the path information is more specific to the URL/URI and querystring, etc in HttpServletRequest as compared to the server and ports in ServletRequest.

String  getMethod()
 
String  getPathInfo() 
 
String  getQueryString() 
 
String  getRemoteUser() 
 
String  getRequestURI() 
 
StringBuffer  getRequestURL() 
 
String  getServletPath() 
 
String  getContextPath()

3. You can create a session from within the request object. The getSession() method will always return a HttpSession object.

HttpSession  getSession( boolean create )
 
HttpSession  getSession()

4. You can get the cookie information throught the getCookies() method. But you will not be able to get a single cookie. Instead the method will return an array of all associated cookies and then you would have to loop through each.

Cookie[]  getCookies()

5. There are some convineance methods which are used to find if the user client supports cookies or not.

String  getRequestedSessionId()
 
boolean  isRequestedSessionIdFromCookie() 
 
boolean  isRequestedSessionIdFromURL() 
 
boolean  isRequestedSessionIdFromUrl() 
 
boolean  isRequestedSessionIdValid()

6. You can authorize the user with 3 methods provided for security within the request object.

String  getAuthType()
 
boolean  isUserInRole( String role ) 
 
Principal  getUserPrincipal()

Coming Up:
ServletResponse and HttpServletResponse


Stumble Digg Technorati Subscribe Delicious