?
Solved

what is the life cycle diffrence?

Posted on 2003-02-28
14
Medium Priority
?
389 Views
Last Modified: 2008-02-26
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
Comment
Question by:pnagasuresh1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8046366
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
 

Author Comment

by:pnagasuresh1
ID: 8046463
If I write a jsp page. This will be converted to servlet. That genarated servlet is HttpServlet or GenricServlet???????????

suresh.
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8046485
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:pnagasuresh1
ID: 8046604
what do u mean by container class. I know genarated servlet file from jsp extends HttpJspBase. Again what is the concept of HttpJspPage?????????
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8046740
>>> 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
 

Author Comment

by:pnagasuresh1
ID: 8046811
Generated servlet from my jsp page is extending HttpServletBase not HttpServletPage.

0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8046846
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
 
LVL 2

Accepted Solution

by:
functionpointer earned 80 total points
ID: 8046861
I assume you meant HttpJspBase, not HttpServletBase
0
 

Author Comment

by:pnagasuresh1
ID: 8057871
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8061525
>>> 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
 

Author Comment

by:pnagasuresh1
ID: 8065428
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
 

Expert Comment

by:CleanupPing
ID: 9059005
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9677808
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question