wood burning stoves 2.0*
The moose likes Servlets and the fly likes Difference between ServletRequest & HttpServletRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference between ServletRequest & HttpServletRequest" Watch "Difference between ServletRequest & HttpServletRequest" New topic
Author

Difference between ServletRequest & HttpServletRequest

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Hi Everyone

I have couple of doubts, they are as follows:
Que1 - What is the difference between the request/ response objects of these two interfaces (ServletRequest & HttpServletRequest) ?
Que2 - When a request is send from client to the server, then that request object is whose object ServletRequest or HttpServletRequest ?

Thanks in Advance
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

1. Http. Servlets are actually generic components designed to support any sort of operation that operates in a request/response cycle. That mostly means HTTP, but ages ago, IBM mainframes routinely did their online apps in a framework known as CICS (Customer Information Control System). It didn't use tcp/ip (not invented yet), and it didn't use HTML or HTTP (also not invented yet), but it was (mostly) request/response. So had servlets - or Java - existed back then, they would have been appropriate.

What an HttpServlet adds to the core servlet class is support for things specific to HTTP. For example, how URLs are processed, the difference between request types (GET, POST, HEAD, DELETE and so forth).

2. Both (an HttpServletRequest is a subclass of ServletRequest). When an HTTP client sends an HTTP(S) URL request to a J2EE webapp server, the server digests the incoming request and constructs an HttpServletRequest and corresponding HttpServletResponse object. Among other things, such as matching up sessions, if applicable.

Don't forget that all of the above are Interfaces, not classes, so the server's specific implementation class will implement the interface, but its properties and behaviours above and beyond what those interfaces demand are server-specific.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Thanks for the reply Tim but still i am not get it clearly .

According to me GenericServlet is an Abstract class which implements interfaces like servlet & servletConfig & this class implements all the abstract methods of the interfaces it implements except the Service(Servletrequest, ServletResponse) method. This is because to keep GenericServlet class common to all the type of request protocols. Now, HttpServlet class which is also an abstract cl;ass extends genericServlet class and implements the service method in it. In this service method there is Http protocol specfic code which enables the servlet to handle Http requests. And this HttpServlet class is the class which we extend in our Servlet class..

Right till now ?

Now say, a http request comes to the container & servlet initilization is done and then the init method is invoked ...after that comes the service (ServletRequest, ServletResponse) & then the doGet/doPost (HttpServletRequest, HttpServletResponse).
That means firstly request becomes sevletRequest & then it becomes HttpServletRequest ??

Is this concept right ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

When you create a class that implements HttpServlet, it also implements GenericServlet, since GenericServlet is the superclass of HttpServlet. For most web applications, however, you would not be subclassing GenericServlet directly, since if you did and you wanted the extra features of HttpServlet, you'd have to supply them yourself.

Probably the most important feature that HttpServlet adds to GenericServlet is that its implementation of the service() method looks at the incoming URL and dispatches it to a service-specific HttpServlet overridable method. For example, a GET gets dispatched to goGet(), a POST to doPost(), and so forth.

HttpServletRequest is likewise a subclass of ServletRequest. But, as I said, these are interfaces. So, for example, Tomcat might actually define a class TomcatServletRequest that implements HttpServletRequest, and therefore (via because of inheritance) ServletRequest.

The GenericServlet class service() method takes as parameters, ServletRequest and ServletResponse. However, eligible parameters also include anything that subclasses those classes - including HttpServletRequest and HttpServletResponse. And anything that implements those interfaces, such as TomcatServletRequest and TomcatServletResponse.

So the webapp server (Tomcat, in this case) wouldn't need to do a lot of constructing and re-constructing. Just create its native objects according to the J2EE spec and everything takes care of itself.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Thanks Tim that was a great help...Cheers!!!
 
 
subject: Difference between ServletRequest & HttpServletRequest
 
Similar Threads
How to implement authentication for each jsp page
Error in Head First Servlet JSP
JSP's request scope and request implicit object
implicit request object type
Sturts dataBase connection problem