• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

what is the life cycle diffrence?

In a servlets we can extend to HttpServlet or GenricServlet.

if it is second case life cycle is
init()
service()
distroy()
if it is HttpServlet what is the life cycle? why I got this doubt is we don't write service() method if we are extending HttpServlet.we will write doGet() or doPost() only.

suresh
0
pnagasuresh1
Asked:
pnagasuresh1
1 Solution
 
functionpointerCommented:
the cycle is the same. doGet and doPost will be called if implemented, but if not, the service method is called.

HttpServlet is just smart enough to differentiate between HTTP GETs and POSTs and call the methods accordingly, if you care to do so. It seems the HttpServlet base implementations of both doGet and doPost methods call service, unless you override them to do something else.
0
 
pnagasuresh1Author Commented:
If I write a jsp page. This will be converted to servlet. That genarated servlet is HttpServlet or GenricServlet???????????

suresh.
0
 
functionpointerCommented:
Your compliled jsp is neither. It is an HttpJspPage which extends javax.servlet.jsp.JspPage which directly extends Servlet.

The abstract JspPage only has  jspInit and  jspDestroy methods.
The abstract HttpJspPage only has a _jspService( req, res ) method.

Your containers implementation will have a class that extends HttpJspPage. This class has a service( req, res ) method that calls the abstract _jspService method, which your compiled jsp must implement.

So, to answer your question, it is not a GenericServlet OR an HttpServlet. Bizarre, eh?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pnagasuresh1Author Commented:
what do u mean by container class. I know genarated servlet file from jsp extends HttpJspBase. Again what is the concept of HttpJspPage?????????
0
 
functionpointerCommented:
>>> what do u mean by container class
I mean the container's class that finally implements HttpJspPage. This will depend on the AppServer/JspEngine/ServletContainer/whatever you are using.

The abstract interface HttpJspPage simply enforces an implementation of a _jspService method. Its just part of the architecture. It gives a jsp engine implementation a place to begin.

In reality, your jsp pages are not servlets. They define the behavior for A servlet. The real 'servlet' instance is in the app server ( mine so happens to have a JspServlet class which extends HttpServlet ). Somewhere in the thick of it all, the app server's servlet knows it can call _jspService on your compiled jsp when it's server/servlet decides that is the necessary resource.
0
 
pnagasuresh1Author Commented:
Generated servlet from my jsp page is extending HttpServletBase not HttpServletPage.

0
 
functionpointerCommented:
Au contraire, mon frere,

This tells me you are using Jasper/Jetty. And from the javadoc:

org.apache.jasper.runtime
Class HttpJspBase
java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--org.apache.jasper.runtime.HttpJspBase
All Implemented Interfaces:
HttpJspPage, JspPage, java.io.Serializable, Servlet, ServletConfig


0
 
functionpointerCommented:
I assume you meant HttpJspBase, not HttpServletBase
0
 
pnagasuresh1Author Commented:
hai function,
I am attending interviews so they are asking different quesions. That is why I am asking my doubts in this manner.


when we use doPost,doGet,doTracd,doPut,doHead ?

suresh

0
 
functionpointerCommented:
>>> when we use doPost,doGet,doTracd,doPut,doHead ?

These map directly to HTTP/1.1. They give you the flexibility to trap these requests and change the response based on the HTTP method of the client. Cheesey example, you can submit form data by POST or GET. Let's say you want to discourage use of GET because you don't want some data passed in the URL encoding. so doGet routes requests to an error page, while doPost performs some normal operation.  You can use them inclusively or exclusively.

IMO, if I would ask someone about using these methods during an interview to see if they understood there purposes, when they would be called, and simply to see if the person knew anything about HTTP or servlet programming. I think what's important to know is that they exist and when and how to use them.  You can always call the HttpServletRequest.getMethod() in your service() method and branch from that as well. True, one can make an argument to implement EVERY one of these methods in your servlet, but you have to ask yourself "Does this app even care?".

Look, there's usually a ton of stuff you want to do for every request, regardless of HTTP method. You can implement doPost, doGet, etc., and put all of this logic in its own "doRegardlessOfHttpMethod" method and then call this from every doPost, doGet, doPut, etc.  If you ask me, it just convolutes your code.
Instead, why not put the base request logic in service, and override the "do's" as necessary, finally calling ( or not calling ) the base service( req, res ) method.  Just because you can make something complicated doesn't mean you always should. Imagine implementing every "do" method to the hilt, then having to change them in a future release.

Sorry for rambling. I am not sure if I answered you question or not.  These methods 'do what they do' and when its appropriate to use them is entirely up to the needs of the application.
0
 
pnagasuresh1Author Commented:
I know when we will use doGet and doPost. But I want to know when should we use doPut,doTrace,doHead,doDelete?
May be we use at diffrent situations.Let me know one example of each,  when will we use them?
0
 
CleanupPingCommented:
pnagasuresh1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
TimYatesCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept functionpointer's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now