Servlet vs JSP

ok - "mention one situation where using a servlet is better than using a JSP. why?"

how would you answer such a question. I looked up the net about this but i didn't find a suitable answer. a jsp is converted to a servlet, so there is very little difference between the two...

any ideas guys!?

[313 byte] By [Guy007a] at [2007-11-26 15:27:57]
# 1

the question can be answered by looking at the purpose of both: it is true that a JSP is translated into a servlet eventually, but to the developer they have a vastly different purpose than a servlet. JSP's were initially introduced to make it easy to include content in the java source, something which is a bit of a chore to do in servlets. That has evolved into a more specific relationship: JSP's are now used to handle the generation of the user interface and the handling of gui events, while servlets take care of executing business logic, through other classes.

So if you would do a database transaction for example: that will be done through a servlet, while the displaying of the results is done in a JSP.

gimbal2a at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
so JSP=generation of page content, Servlet=some logic...but why can't you (or rather, should not) put the logic in the jsp directly?thanks
Guy007a at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Nowadays, almost all web applications are designed according to MVC (Model-Viev-Controller) pattern. Usually servlets act as Controller and JSPs might be a View. It means that all business logic is invoked from the servlet, the all necessary data is put into request/session context and transfered to the presentation layer which in this case is a jsp page. The jsp page just use this data to render page, and there are no any "business logic" like connecting to DB, whatever. Just presentation activity using JSP/JSTL tags like <c:foreach>.

In other words, this two APIs could be used to complement each other, rather than as two alternatives.

Look here: http://www.stardeveloper.com/articles/display.html?article=2001060501&page=1

agomilkoa at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
> but why can't you (or rather, should not) put the> logic in the jsp directly?It violates MVC design architecture. If you want to build scalable and maintainable application you have to follow it.
agomilkoa at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
One situation is that a servlet is precompiled into a class file. A .jsp is typically compiled at run-time. So a compilation error will be caught prior to deployment for a servlet, and at runtime for a .jsp
jleecha at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Here's another situation where a servlet is better than a .jsp.A servlet can use an Outputstream, or a PrintWriter. Whereas a .jsp can only use the PrintWriter. So a servlet can do things like output images, whereas a .jsp cannot.
jleecha at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
And it's horribly difficult to debug problems in JSP with scriptlets. You miss one <% and you're dead. The indentation of your Java code never lines up with the indentation of your HTML, so that makes it even harder to read. It's a mess at the best of times.
DrClapa at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

hi Guy007

First of all let me tell you that servlet and JSP is not twi different things. In reality JSP first converted into Servlet then it is being copiled. I hope this thing would be very clear to you.

Now let us see why evolution of JSP necessary? To anwser this question I would like to say that imagine a situation that you have to write 250 line of HTML code using servlet's , simply you will be using response.getWriter().println() for these 250 line and now after one month you have to change CSS of particulat say font element of HTMl, Imagine how tedious it would be to search in that complicated servlet to find that particular tag and change its css. For developer's sake of simplicity JSP are used so that maintainability becomes easy.

And generally we used to keep JSP pages on public folder of web application or even if you are not keeping then it is possible that source code of your page is visible. So for saving our business logic from misuse we use servlet's as our business component and JSPs as presentation.

For the support of my above case imagine your JSP is having logic to connect to database. Now anyone can have the information of your database server along with a lots of info. And again it is not desirable.

I hope this cases would have solved your query,

Thanks,

Gaurav Daga

gauravda at 2007-7-8 21:43:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...