• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp/servlet/container relationships

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tnx, what about the other question?>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question is unclear.
Refer Hello World Servlet example.

Use Real Words. What is tnx?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no problem. Didn't know (about the "real words").

About the question, it was a theoretical one.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get your answer?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"The Joker " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic