Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets Vs JSP

 
nalini peddareddy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?


Regards,
P.N.Reddy
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover, you can search the same forum or a servlet forum in order to get more results.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Pie
Posts: 1671
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic