Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Servlet working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet working" Watch "Servlet working" New topic
Author

Servlet working

Animesh Gupta
Greenhorn

Joined: Jul 14, 2010
Posts: 10
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.

<code> public SimpleServlet extends HttpServlet {

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
getServletContext().log("init() called");
count=0;
}

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
getServletContext().log("service() called");
count++;
response.getWriter().write("Incrementig the count: Count = "+count);
}

@Override
public void destroy() {
getServletContext().log("destroy() called");
}
}
</code>

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 ?

I will really appreciate any help from you all.

Thanks and Regards
Animesh



Thanks and Regards
Animesh.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Animesh Gupta wrote:Hi All,
I am very new with servlet programming.

Welcome to the Ranch.

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.

public void init(ServletConfig config) throws ServletException {
super.init(config);

Generally you will override the parameter-less version of init(). That way you don't have to worry about calling super.init().

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 ?

It doesn't have to. That's the way that Java works.

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 ?

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

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
Animesh Gupta
Greenhorn

Joined: Jul 14, 2010
Posts: 10
Hi Bear Bibeault,
Thank you for the prompt reply.

I have a couple questions to put up.

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.

Thanks and Regards
Animesh.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Animesh Gupta wrote: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 ?

Sure; read the source code of something like Tomcat (there's also a book that discusses how Tomcat works, although I don't know which Tomcat version it's based on).
So, how does servlet container gets to know that it has to create an instance of my class, i.e., the class SimpleServlet ?

Your classes are configured in the web.xml file (web application configuration file).
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
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.

Thanks and Regrads
Animesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet working