What is the difference between GenericServlet, HttpServlet and a Servlet? - java

I was searching for exact difference between javax.servlet.http.HttpServlet
, javax.servlet.GenericServlet and javax.Servlet unable to find it out.
"Exact Difference" means
Usage
Reason behind javax.servlet.GenericServlet existence

"Exact difference" meaning what? The API lists the exact differences.
Servlet is an interface defining what a servlet must implement.
GenericServlet is just that, a generic, protocol-independent servlet.
HttpServlet is a servlet tied specifically to the HTTP protocol.
Are you asking when you'd use any of those?
In general, you'd extend HttpServlet to implement an application's web layer.
You might implement Servlet if you're writing your own container or handling everything yourself. You might extend GenericServlet to handle a different protocol, but you might not.

javax.servlet
Servlet is a server-side web technology. As the name implies, it serves a client request and receives a response from the server. You have to implement javax.Servlet (Interface) to handle a servlet work.
javax.servlet.GenericServlet
Signature:
public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
GenericServlet defines a generic, protocol-independent servlet.
GenericServlet gives a blueprint and makes writing servlet easier.
GenericServlet provides simple versions of the life-cycle methods
init and destroy and of the methods in the ServletConfig interface.
GenericServlet implements the log method, declared in the
ServletContext interface.
To write a generic servlet, it is sufficient to override the
abstract service() method.
javax.servlet.http.HttpServlet
Signature:
public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
HttpServlet defines a HTTP protocol specific servlet.
HttpServlet gives a blueprint for Http servlet and makes writing
them easier.
HttpServlet extends the GenericServlet and hence inherits the
properties GenericServlet.

javax.servlet.Servlet is interface, it defines methods for all the implementations - that's what interfaces usually do.
javax.servlet.GenericServlet is protocol independent. It is abstract, so it is not to be directly instantiated. It is usable class to extend if you some day have to write servlet for protocol other than HTTP.
javax.servlet.http.HttpServlet is abstract class to be extended if you want to communicate over HTTP protocol. Most likely you only have to care about this one.
More exact information you can find behind the links.

-> One common feature is, both these Classes are Abstract Classes.
-> GenericServlet is a super class of HttpServlet class.
-> The main difference is that, HttpServlet is a protocol dependent whereas GenericServlet is protocol independent. So GenericServlet can handle all types of protocols, but HttpServlet handle only HTTP specific protocols.
-> GenericServlet belongs to javax.servlet package. HttpServlet belongs to javax.servlet.http package
-> GenericServlet is an abstract class which extends Object and implements Servlet, ServletConfig and java.io.Serializable interfaces. HttpServlet is an abstract class which extends GenericServlet and implements java.io.Serializable interface.
-> GenericServlet supports only service() method does not contain doGet() and doPost() methods. HttpServlet support also doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).

Servlet:-
The Servlets runs as a thread in a web-container instead of in a seperate OS process.
Only one object is created first time when first request comes, other request share the same object.
Servlet is platform independent.
Servlet is fast.
GenericServlet:-
General for all protocol.
Implements Servlet Interface.
Use Service method.
HttpServlet:-
Only for HTTP Protocol.
Inherit GenericServlet class.
Use doPost, doGet method instead of service method.

HttpServlet is specific to the HTTP protocol and hence it supplies methods for the HTTP verbs: doGet, doPost, etc, and a version of the generic service method that takes HTTP-specific request and response objects. It is a special type of Servlet which is actually a pretty minimal interface.
GenericServlet is the basic, protocol-neutral implementation of the Servlet interface. Often you'll find similar basic implementations of interfaces in an API; in this case GenericServlet adds a bit of functionality to the Servlet API: getServletName, getServletInfo, and pass-through methods for the servlet init parameters. HttpServlet benefits from these additions by extending GenericServlet.
Generally everyone coding against this API is using HttpServlet for implementing HTTP web services, but one can also extend or use GenericServlet for implementing server/service functionality using a custom protocol, or another extant protocol, for example, FTP.

Servlet is an interface which contains five abstract methods in order use servlet we have to provide an implementation for all these five methods, which is a headache. In order to avoid this complexity, we have GenericServlet and HttpServlet for next level.
GenericServlet is protocol independent, it means it can accept any protocol request.
GenericServlet can forward and include a request but we can not redirect the request.
Session Management with cookies and HttpSession is not possible in GenericServlet.
In GenericServlet it is not possible to define separate logic for get and post request.
HttpServlet is protocol dependent. it means, it accepts only HTTP protocol request.
HttpServlet can forward and include and redirect a request.
Session Management with cookies and HttpSession is possible in HttpServlet.
In HttpServlet it is possible to define separate logic for get and post request.

Servlet interface
Its the super interface for GenericServlet and HttpServlet. It contains 5 abstract methods and all inherited by GenericServlet and HttpServlet.
Problem
If you want to create servlet by implementing Servlet interface all the 5 abstract methods of the interface Servlet should be overridden eventhough Programmer is not interested
Usage
Only implement Servlet interface if you like to develop your own container.
GenericServlet
Its the immediate subclass of Servlet interface and super class of HttpServlet. In this class, one abstract method exist: service(). Other 4 abstract methods are implemented in this class.
Problem
All methods are concrete except service() method, So you have to implement it as a callback method.
Usage
It is protocol independent so Can be used with any protocol like FTP, SMTP, HTTP etc.
HttpServlet
Its subclass of both GenericServlet and Servlet interface. Immediate super class of HttpServlet is GenericServlet. HttpServlet does not contain any abstract method. Eventhough the HttpServlet does not contain any abstract methods, it is declared as abstract class by the Designers to not to allow the anyone to create an object directly because a Servlet object is created by the Servlet Container.
Problem
HttpServlet is protocol dependent and used specific to HTTP protocol only.
Usage
service() method need not be overridden. It can be replaced by doGet() or doPost() methods with the same parameters. Its most used method to create servlet.
hierarchy
Reference: way2java.com

-->GenericServlet can process multiple clients request from a single form. Whereas, HttpServlet can process multiple clients requesting from multiple HTML forms.
--> GenericServlet is Stateless and HttpServlet is Stateful.

All classes, interfaces, and methods present in the javax.servlet package are protocol independent (generic to all protocols).
In contrast, all classes, interfaces, and methods present in the javax.servlet.http package are protocol dependent (specific to the HTTP protocol)

http servlet is an class which is specific and allows only http protocol not allowed to other protocol like ftp smtp
generic servlet are allowed all types of protocol only one abstract method that is service method and other four method are concrete method
generic servlet is the superclass of http servlet

Related

Undertow: Using existing Servlet instance

I am trying to replace in an existing application the embedded web server with Undertow as the old does not work correctly in some cases.
The embedded web server is used to server a few simple servlets. My main problem is that I can't find a way to register an existing HttpServlet instance in Undertow.
All API methods I was able to find only accept an Class<? extends Servlet>. Also the servlet tutorials and others only use Class<? extends Servlet> for registering a servlet.
My Servlets however already exists and I need to use them directly as each servlet is already configured with certain parameters and thus can not be created by just providing a class name.
Is there any way to use an existing instance of a class that implements javax.servlet.Servlet directly in Undertow or is Undertow not usable for such a case?
It is possible to use an existing Servlet instance in Undertow by using the method Servlet.servlet(String, Class<? extends Servlet>, InstanceFactory<? extends Servlet> servlet):
Servlet myServletInstance = ...
Servlets.servlet("MessageServlet", MessageServlet.class,
new ImmediateInstanceFactory<Servlet>(myServletInstance))
.addMapping("/*")
In this case the class parameter is ignored and instead of creating a new instance via constructor by Undertow internally, the provided servlet instance is used.

what is the type of request and response object generated by container?

As it is written in the book when a request comes for a Servlet, a request and response object is generated by the container and put into the service method as an argument.
HttpServletRequest
HttpServletResponse
ServletRequest
ServletResponse
are Interface so they can't be instantiated.
So
what is the type of the object?
What information does the request and response method contains?
How does the container know it is a get or post request?
Whatever type the container chooses to instantiate, conforming to the interfaces you list.
Whatever information the interface exposes, plus whatever the implementation provides. To access non-interface functionality you must cast, of course.
... By looking at the request. Read the HTTP spec.
You should review the Java EE Tutorial - Chapter 15 Java Servlet Technology and remember that JSP(s) are compiled into Servlet(s). For web based requests,
The container's implementation of the interface.
See the ServletRequest and ServletRepsonse Javadocs.
By calling the Servlet.service(request, response), the container has the default behavior of parsing the request and calling doGet() or doPost() (or one of the other methods from Servlet).
You may need to look into ServletResponseWrapper, HttpServletResponseWrapper, ServletRequestWrapper & HttpServletRequestWrapper under package javax.servlet.http. In addition, the web container implementation may subclass those wrapper classes or implement those interfaces
And look into ServletRequest and ServletResponse interface in above api doc.
Look into service() method as explained in #Elliott Frisch's answer.

Basic on servlet

When do we implement Servlet Class and When do we implement GenricServlet class as both of them are for life cycle methods?
and my sir told me that GenericServlet class is a helper class,so if we have to use GenricServlet class then whats the need of the Servlet interface?
Do we use Servlet class also without the help of GenricServlet?
Servlet is an interface and GenericServlet is a abstract class which implements Servlet, ServletConfig & Serializable.
GenericServlet class provides default implementation for all the methods in Servlet interface except the service method. So if you extends your servlet class from GenericServlet, you can only implements the service method and override those methods that you care about. It saves some time.
And of cause you can implement Servlet class without the help of GenericServlet.
Servlet is an interface defining what a servlet should implement.
GenericServlet is a generic, protocol-independent servlet.
HttpServlet is a servlet tied specifically to the HTTP protocol.
when to use
In general, you'd extend HttpServlet to implement an application's web layer.
You can also implement Servlet if you're writing your own container or handling everything yourself.
You can extend GenericServlet to handle a different protocol,by using the features provided by the container, but you might not.

How do i call Post method of HttpServlet from GWT servlet?

I have two servlets, one is a GWT Servlet which extends RemoteServiceServlet and second is BasicServlet which extends HttpServlet. So my question is how do i call post method of HttpServlet from GWT Servlet. And here I confirm, I don't want to redirect to servlet using RequestDispatcher, I want to create a new Post call with new parameters.
Please guide me in right direction.
TIA!!!
Seems like an odd thing to do. Can't you put the code you want to execute in a third class/service and call it directly?
If they are on different servers (or some other reason) then you could use HttpClient (or similar)?

Why doGet(), doPost() Methods are "protected"?

I'm confused about the access modifier of the doGet(), doPost() and other methods of HttpServlet class.
Why are they protected?
As per my understanding, the protected modifier of doGet() means that a client has to be in the same package (or a child - through inheritance) to access doGet(). So how will the invoking JSP or the container access it?
They're protected primarily for two reasons.
So that external classes can't just call them, like you reasoned. Technically, there are ways to get around method visibility modifiers using Java Reflection (if the Security Manager allows it or there is none), but ordinarily, a protected method can only be accessed by classes in the same package or by subclasses, which brings me to point #2.
So that subclasses or concrete implementations of HttpServlet can override them. Well, they can also be overridden if they were public, but see point #1.
Now your other question, "So how will the invoking JSP or the Container access it?"
HttpServlet implements the Servlet interface, which declares a service(ServletRequest, ServletResponse) method. This, of course, by default becomes public in HttpServlet. This is the primary entry point (for containers) to call into HttpServlet implementations.
My guess (I haven't dived into the source) is that the default implementation of HttpServlet checks the ServletRequest object passed in, which is actually an HttpServletRequest and which defines a getMethod() method that returns the HTTP method used. It then dispatches to either doGet() or doPost() depending on the HTTP request method.
Here's from the official javadoc.
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself
And also
There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
And in the docs for doGet method:
Called by the server (via the service method) to allow a servlet to handle a GET request.
So HttpServlet is designed for inheritance and the entry point is the service method. Hence doGet is protected to enforce clear API.
doGet and doPost are the basic methods in generating and sending the HttpResponse to the client (i.e usually Browser or HttpClient)
Also, The container calls the Servlet.service() method which is public. It then calls the HttpServlet.service() method which is protected and it then call doGet()/doPost() method.
Suppose I am having a class MyClass which is not a servlet, then do I want my class to have methods doGet and doPost? Well if it's not a servlet then how can it respond or capture any web based requests.
Only servlets can capture and respond to web based requets.
So it makes sense that I will be able to capture and respond to web based requests only if my class extends Servlet and and hence I will be able to use doGet, doPost and variuos other methods.
I think you are wondering about how the servlet container calls the protected method doGet and doPost.
Actually there is an interface named javax.servlet.Servlet. A class named GenericServlet implement the interface. And the HTTPServlet class extends this GenericServlet.
When there is an http request to an HTTPServlet, the container simply use the method service(...) declared in the interface. That method is public. Then in the GenericServlet, the service method called doGet and doPost. If your servlet class extends the HTTPServlet class and override the doPost method, then this method will finally get called.
Think of protected as an invitation to override a method. You are deriving a class from HttpServlet so these methods are the ones to override. They all have a default action so you can just override the methods which are of interest to your application.

Categories

Resources