This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlets vs JSP" Watch "Servlets vs JSP" New topic
Author

Servlets vs JSP

Sandipan Mukherjee
Greenhorn

Joined: Dec 07, 2006
Posts: 8
What are differences between Servlets and JSP?
please reply..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

"Sandy",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Both are used for generating Dynamic Web Content.

Main Difference is: Servlet can contain only Java Code but JSP can contain mixture of both html as well as Java Code.

Place the same question in google search box.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Ajay Vikas
Ranch Hand

Joined: Nov 08, 2006
Posts: 66
But Even servlet can support HTML output to be displayed as JSP.
Vid Srini
Ranch Hand

Joined: Aug 17, 2006
Posts: 35
Then main difference I feel is

1)user doesnt need to recompile the JSP files.Container takes care of it.Where as in case of SERVLET,user needs to recompile if any change is made to code and also he needs to restart the server.

2)Next is,Writing html code in Servlet is difficult.Because it has to be embedded inside Java and need to recompilation even for a small change.
In case of JSP,html tags are directly used.


Hope this gives answer to your question.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

But Even servlet can support HTML output to be displayed as JSP.


what would happen if you want to generate 1000 line HTML as a response?



2)Next is,Writing html code in Servlet is difficult.Because it has to be embedded inside Java and need to recompilation even for a small change.
In case of JSP,html tags are directly used.



If you modify JSP, it is even worse than servlet. why? before compilation it needs to go first translation process to convert to servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

The point shouldn't be which one automatically compiles, or which one needs to be translated, or any other behind-the-scenes technical details. Each of JSP and servlets has a distinct place in a web application. Using one when the other is appropriate just because of some implementation detail is not the way to create robust and well-designed applications.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

This question comes up often enough that I have added a new entry for it in the JSP FAQ.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
I have in fact never seen a well designed web application that didn't employ both (or at most replaced JSP with another display technology like XSLT or JSF).

I always wonder why so many people seem focussed on selecting a single magic bullet that will solve all their problems, instead of a combination of things that will do it so much better.


42
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Originally posted by Jeroen T Wenting:
I always wonder why so many people seem focussed on selecting a single magic bullet that will solve all their problems, instead of a combination of things that will do it so much better.


I believe there are a number of reasons that we see so much of that:

Novice programmers that have learned one technology or the other (usually JSP) and use it exclusively since they aren't famililar with the other.

or

Programmers who makes decisions based upon specious factors such as "you don't have to compile JSPs" and use them exclusively just to get out of having to compile Java code.

or

Know-nothing CTO's or other people in power who don't bother to keep current and make rules that make no sense.

or

Those who just haven't learned any better yet.

We're doing our best to reach them all!
[ December 15, 2006: Message edited by: Bear Bibeault ]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Were it just related to Java (or even IT) that would be the obvious list of reasons indeed.
But the same thing can be observed in every aspect of life.
Companies deciding to allow travel only using a single airline and hotel chain for example, even if other choices make more sense in many cases (sometimes cutting days, more often many hours, off of travel schedules).
People taking the car even if using a bus or train on a particular route makes more sense (I see that every day, commuting a route where the train is about 10% cheaper and 50% faster than the car yet droves take the car anyway).
Always watching the same TV channel, even if there are better ones for specific purposes (like a news channel to catch the news, or a dedicated movie channel that's showing your favourite movie that night while your preferred channel shows some soap opera rerun you've seen a dozen times already). My parents are somewhat like that, they're so focussed on the idea that German television is superior to Dutch that they'll watch a dubbed movie on a German channel and complain about the dubbing when that same movie is shown at the same time on a Dutch channel in the original with subtitles.
Or always shopping at the same store, even if another store has superior goods of some kind at a lower price and is right next door.

There must be more to it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlets vs JSP