File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Servlets Vs JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlets Vs JSP" Watch "Servlets Vs JSP" New topic

Servlets Vs JSP

nalini peddareddy

Joined: Jul 18, 2005
Posts: 16

I know every JSP is translated into Servlet and JSP could better act as a presentation layer. A JSP can do all that a Sevlet can do. Then what is the main difference between both of them? Servlets are preferred to be used for business logic. Why not JSP's when JSP also is finally a servlet?

Could anyone please clear my doubt?

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Because if you'll use an MVC layout, you'll have to keep your presentation separated from your logic (you've said you already know it)

Why not JSP's when JSP also is finally a servlet?

There are so much things you can do nowadays This is good and bad. Why ? Because you can also do a lot of crap. You may use a JSP to act as a servlet. After all, there's also a tag in web.xml for it. But, should you really use it ?

[My Blog]
All roads lead to JavaRanch
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Moreover, you can search the same forum or a servlet forum in order to get more results.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

A JSP isn't really just "a servlet".

Think about it this way - when you write a JSP page, you aren't really writing a servlet. It's more like a template - you provides a mix of HTML telling how the page looks, along with tags to let the page "hook into" other parts of your web application. Later the application server comes along and uses this template that you wrote to produce a servlet that is specialized for displaying the HTML page you wrote in the template.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
A JSP is an HTML page with bits of java embedded in it.

If you had to write that as a servlet manually, you would have a heck of a lot of out.println() statements, and it would be a nightmare to maintain.

Thats basically the difference. Servlets were "invented" first. JSPs came about because people wanted an easier way to write servlets that were output only.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

Just because JSPs are implemented using servlets does not mean that they are interchangable. As Nathan pointed out, JSP is a templating technology for delivering text-based output to browsers.

There is no "versus" -- JSP and servlets are complimentary technologies, not competing. A well-written web application uses both to their best advantages.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Servlets Vs JSP
It's not a secret anymore!