Servlet working

Hi All,
I am very new with servlet programming. I have a confusion regarding the way servlets work.

1.) I have seen the servlet API and whatever I see, like, Servlet, HttpServletRequest, HttpServletResponse, ServletRequest, ServletResponse, are all interfaces. Where is the concrete implementation for these interfaces ?

2.) I have read all around that servlet container creates the instance of the servlet. But, how does it do when javax.servlet.Servlet is an interface ?

3.) The servlet container calls the init(), service and destroy methods for a complete servlet lifecycle. Let me take an example.

My question is how does the servlet container know that init(), service() and destroy() methods are overridden under the class SimpleServlet, i.e., my custom class ?

The servlet container creates and instance of javax.servlet.Servlet to call init(), service() and destroy() methods and not an instance of my class SimpleServlet. Then how does these methods get called ?

1.) I have seen the servlet API and whatever I see, like, Servlet, HttpServletRequest, HttpServletResponse, ServletRequest, ServletResponse, are all interfaces. Where is the concrete implementation for these interfaces ?

The concrete implementation for all of those (except for Servlet, which you provide) is provided by the container; Tomcat for example. The fact that they are interfaces means that you have no need to be concerned about the concrete classes. You always code to the interface.

2.) I have read all around that servlet container creates the instance of the servlet. But, how does it do when javax.servlet.Servlet is an interface ?

You will proved concrete classes for the servlets by extending HttpServlet.

The servlet container creates and instance of javax.servlet.Servlet to call init(), service() and destroy() methods and not an instance of my class SimpleServlet. Then how does these methods get called ?

By the way, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

The concrete implementation for all of those (except for Servlet, which you provide) is provided by the container; Tomcat for example. The fact that they are interfaces means that you have no need to be concerned about the concrete classes. You always code to the interface.

1.) I want to see the implementation to know how the servlet container creates instances and invoke the servlet life-cycle methods. Is there a way to know this ?

No, the container creates an instance of your SimpleServlet class.

2.) There is the problem. Unless I provide my custom class, it doesn't exists. So, how does servlet container gets to know that it has to create an instance of my class, i.e., the class SimpleServlet ? Let us suppose if the servlet container creates an instance of Servlet and then downcasts it to my class, i.e., the SimpleServlet, then shouldn't there be a ClassCastException beacuse of down-casting ?

I am sorry if you find these questions very basic. But, I am feeling little curious to know the mechanism how this thing works end to end.

Let us suppose if the servlet container creates an instance of Servlet and then downcasts it to my class, i.e., the SimpleServlet, then shouldn't there be a ClassCastException beacuse of down-casting ?

No, and this goes back to some misconceptions you seem to have about Java-the-language. It creates an instance of *your* class. Your class is-a Servlet.

Animesh Gupta
Greenhorn

Joined: Jul 14, 2010
Posts: 10

posted Jul 14, 2010 14:04:01

0

Hi David Newton,

Thanks for the reply. I am kind of new to servlet programming so little bit beating around the bush.

Anyways you helped me to see things little more clearly regarding the servlets.
I will keep posting doubts as I am relatively new to servlet coding.