ServletRequest vs HttpServletRequest

ServletRequest vs HttpServletRequest


When the client clicks the submit button and sends a request to the server to invoke some Servlet (by giving the alias name of Servlet), the Servlet container loads the servlet and calls the service() method. The container also creates objects of ServeltRequest and ServletResponse and passes them to service() method. The ServletRequest receives the data sent by the client (which the Programmer can extract in the program) and ServletResponse is responsible to send the data required by the client. This we studied in First Example – Login Screen Validation.

ServletRequest vs HttpServletRequest

ServletRequest vs HttpServletRequest

Observe, HttpServletRequest is derived from ServletRequest.

Following table gives the differences between ServletRequest and HttpServletRequest.

ServletRequest HttpServletRequest
Belongs to javax.servlet package Belongs to javax.servlet.http package
sub interface of ServletRequest
Comes with many methods like getParameter() etc. It inherits all the methods of ServletRequest and apart adds its own like getQueryString() etc.
Used in combination with GenericServlet Used in combination with HttpServlet
Protocol independent Protocol dependent and specific for HTTP protocol
When HTTP was not used, in earlier days, when a request came from client, Web container used to create an object of ServletRequest and passed to service() method. When a request comes from client using HTTP protocol, Web container creates an object of HttpServletRequest and passes to service() method

Table of ServletRequest vs HttpServletRequest

Something more to know about ServletRequest vs HttpServletRequest:

1. With HTTP protocol also, GenericServlet and ServletRequest can be used. The container creates an object of HttpServletRequest (because request is coming from client using HTTP) and passes to ServletRequest, if ServletRequest is required by the container. The actual implementation is left to the Web server designers.

2. ServletRequest and HttpServletRequest are interfaces with all abstract methods. The implementation part of abstract methods in the subclasses is written by the Web server designers. The implementation is completely dependent on the Web server. For example, the Weblogic server designer may write his own subclass of ServletRequest as WeblogicServletRequest and can pass to service() method. The extra behaviour and properties are server dependent.

Leave a Reply

Your email address will not be published. Required fields are marked *