The moose likes Servlets and the fly likes why cannot use view in Servlets?Why we go for View in jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why cannot use view in Servlets?Why we go for View in jsp?" Watch "why cannot use view in Servlets?Why we go for View in jsp?" New topic

why cannot use view in Servlets?Why we go for View in jsp?

Ishrayansh Nath
Ranch Hand

Joined: Nov 05, 2004
Posts: 45
I have small doubt.
In MVC,View Why We go for JSP.
Controller Why we go for Servlets.

why can not do exchage to use these two's?

both r doing similar functionalities.

what is reason give me brief details.

Shiryans Nath
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Why wouldn't you want to use them this way?

A servlet is laid out perfectly to handle the control flow in a web programming evironment. It's method names make everything very self evident.
Building the output stream, however, is cumbersome in a servlet.
It involves typing 'out.println' over and over again.

JSP (especially now, with EL and JSTL) makes it possible for people with design skills but no understanding of Java to focus on look and feel in an evironment almost as simple as plain HTML pages. Controlling the flow, however, is cumbersome as all of the underlying servlet structure has been abstracted and buried in order to hide it and simplify things for those only concerned with appearance.
[ May 16, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Aneel Ansari

Joined: May 16, 2005
Posts: 7
i think Ben is right

i go with ben, servlets and JSP are going very fine in their ways.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I think the best answer I can give to a question like this is to advise you to try coding your webapp by putting all of your presentation logic into servlet code rather than a JSP. Then, you'll realize very quickly why we use JSPs.

James Carman, President<br />Carman Consulting, Inc.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Some teams in my shop have these rules: Servlets are pure Java, no HTML and no output to the browser. JSPs are pure HTML and tags, no Java. This works out nicely with a Servlet controller, JSP view design.

No Java in JSP is pretty harsh, but if you can really do it the JSPs are much cleaner. My own project inherited a framework and architecture that uses Java in JSPs and it is painful at best. Given the chance, I'd adopt these rules next time around myself.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
More straight, a JSP is just for rendering the view, on the other hand Servlet works as a controller. Bottomline is, vice versa is a SIN.

If you really want to see the punishment for this sin. Please do the experiment, however James has already invited you. Try it out and you will know.

You can look at JSTL, EL, and custom tag features. These are the evidences that java code in JSP is highly discouraged.
I agree. Here's the link:
subject: why cannot use view in Servlets?Why we go for View in jsp?
It's not a secret anymore!