This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes jsp/servlet/container relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp/servlet/container relationships" Watch "jsp/servlet/container relationships" New topic
Author

jsp/servlet/container relationships

Ori Weisler
Greenhorn

Joined: Feb 26, 2009
Posts: 10
Hello guys,

I understand that a servlet is invoked by the container, and I understand that both Servlets and Jsp are on the server-side.
But I dont understand the relationships between them. I know that I can pass parameters from the servlet to the Jsp file using the
RequestDispatcher and I know that I can pass params from the Jsp using the GET and POST.

Does the container call the jsp? can I define which will be called first (Servlet or Jsp file)?
what is the Life-cycle (:client->container->servlet->jsp)?

Another question: Why should I have more than one Servlet?

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A Java Server Page is a Java servlet. A JSP Engine converts the stuff in the JSP file and creates a servlet. When an HTTP request comes in for the JSP file, the container invokes the corresponding servlet.

Ori Weisler
Greenhorn

Joined: Feb 26, 2009
Posts: 10
ok, I got it.

Whats happen if I makes a class extends HttpServlet and I define in the web.xml the same path as the jsp file?

I guess that if I defined different path, the container invokes two servlets.

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Does the container call the jsp? can I define which will be called first (Servlet or Jsp file)?
what is the Life-cycle (:client->container->servlet->jsp)?

Container? To be precise and technical it is Servlet Container (eg. Apache Tomcat).
JSP will be compiled and converted to servlet. So servlet container will invoke related servlet.
Refer JSP Life Cycle.

Another question: Why should I have more than one Servlet?

It depends on your requirement.
Ori Weisler
Greenhorn

Joined: Feb 26, 2009
Posts: 10
tnx, what about the other question?>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Ori Weisler wrote:ok, I got it.

No, that's not quite it.

Don't confuse the servlet created on behalf of a JSP (which is just an implementation detail) with servlets that you define yourself. In fact, unless you want to go look at that generated servlet for academic purposes, you can just forget that JSPs get turned into servlets.

In modern accepted web app structures, a servlet gets control first as the result of a request. This servlet is known as a "page controller". It performs any data fetching or other activities necessary to show the "view" which is usually defined via a JSP page.

Control transitions from the page controller to its view via the request dispatcher.

The only relationship between the controller servlet and its JSP view is the loose coupling provided by the request dispatcher.

You might benefit from reading this article on web app structuring.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Your question is unclear.
Refer Hello World Servlet example.

Use Real Words. What is tnx?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Ori Weisler wrote:tnx, what about the other question?>
Please use real words when posting to the forums. Abbreviations such as "tnx" in place of "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses. Please read this for more information.

Now, what other question? Having more than one servlet?

Let me ask this back: does your web app do more than one thing? Would you put the code to log into a web application in the same class as the code to place an order, or with the code to move money from one account to the other, or to perform any of the other myriad operations that a web application might allow?

Ori Weisler
Greenhorn

Joined: Feb 26, 2009
Posts: 10
no problem. Didn't know (about the "real words").

About the question, it was a theoretical one.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Did you get your answer?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"The Joker " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Sandeep Kumar Jakkaraju
Ranch Hand

Joined: Feb 25, 2009
Posts: 75
This is a very nice question. When you have one servlet why do you need other one .....
may be the other servlet gives you someother output ........

If you really want one servlet ... then you can be interested in using "struts" framework.....

Thank s
Sandeep
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsp/servlet/container relationships
 
Similar Threads
posting data not using a form
passing parameters from servlet to jsp
A question about servlet initParameter
Accessing the Servlet Init parameters in jsp
3 tiers architecture with WebSphere