Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp/servlet/container relationships

 
Ori Weisler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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?

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tnx, what about the other question?>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is unclear.
Refer Hello World Servlet example.

Use Real Words. What is tnx?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem. Didn't know (about the "real words").

About the question, it was a theoretical one.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get your answer?
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic