Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet working

 
Animesh Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic