Forums Register Login

jsp/servlet/container relationships

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
tnx, what about the other question?>
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Your question is unclear.
Refer Hello World Servlet example.

Use Real Words. What is tnx?
+Pie Number of slices to send: Send
 

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?

+Pie Number of slices to send: Send
no problem. Didn't know (about the "real words").

About the question, it was a theoretical one.
+Pie Number of slices to send: Send
Did you get your answer?
+Pie Number of slices to send: Send
"The Joker " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
+Pie Number of slices to send: Send
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
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1989 times.
Similar Threads
A question about servlet initParameter
passing parameters from servlet to jsp
Accessing the Servlet Init parameters in jsp
3 tiers architecture with WebSphere
posting data not using a form
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:41:41.