File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Difference between JSP and SERVLET?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Difference between JSP and SERVLET??" Watch "Difference between JSP and SERVLET??" New topic

Difference between JSP and SERVLET??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all friends,
Can any one tell me what is the difference between JSP and SERVLET.And What r the advantages and disadvantages over each other.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

This question crops up very often and I recommend a search of the JavaRanch forums where you will find several discussions like this
Khurram Shahood(SCJP2)

Joined: Jun 08, 2002
Posts: 18
The Java Server Side technology was introduced on servlets to reduced the code into HTML tags.
And it is easy to readable as compare to servlets.
And easy to understand.

khurram shahood(Java Farmer)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
JSPs and servlets are container-managed components. JSPs are designed as presentation templates while servlets can be considered as controllers. For more, see the Marty Hall book.
Steven Kors
Ranch Hand

Joined: Jan 30, 2002
Posts: 33
Technically(simply speaking) there is not difference between a JSP page and a Servlet since the JSP is compiled into a servlet by the container that you are using. Fundamentally though, JSPs and servlets are completely different.
Servlets are the work horses of Java web technology, they have the full power of the Java language and can output HTML using the print writer. Essentially your dynamic data will be created/processed using servlets. The problem with servlets is essentially a simple one to understand. To output a single line of HTML you have to do the following;
out.println("Hello"); // Can you imagine 30, 40, 50 , maybe even a hundred of these ... and then having to maintain it?
To output a line of HTML you need to print it out. Now this is not so much of a problem if you are creating a dynamic table or if your web page is very small but for more complex pages that are much larger, you would go crazy with placing these HTML statements into the out.println(); statement above. Not to mention that you need to filter any special characters like the \ or the " . So even though Servlets are powerful they can get tedious with larger pages.

JSP (Java server Pages)were created after Servlets to address the problem of the out.println();. JSPs are formatted much like the typical HTML pages with the ability to use all the major scripting languages. JSP has, like servlets, the access to the full power of the Java language. The important to remember that JSPs are the presentation side of the page you are building. Because of this you should always strive for the separation of Java Code from the JSP. We want to do this so that our HTML developers can edit the page with out having to know any Java code. We can utilize tag libraries and make available tools in that manner but when ever possible we do not use java code in JSP (unless we really really really have to )

Both technologies have their purpose and I am sure that you could use each technology on its own but I have found that together they are a formidable combination in terms of design methodology, flexibility and power. JSP as the presentation and Servlets hard at work, behind the scenes, serving up your dynamic data and processing your complex algorithms.( No Al Gore didn’t invent the Algorithm … ok that sounded funnier in my head )
"For more, see the Marty Hall book." - Anthony
The webpages to his books are called CORE servlets and Java server pages and MORE servlets and java server pages
Here is a link to the SUN tutorials and specifically to the Java Server pages short course and the Servlets short course.
Once you have an understanding of how each technology works, perhaps you should review a design methodoloy like MVC.
MVC == the following;
M = model or the business logic (database access, typically Servlet)
V = view represents what the client sees (JSP)
C = Controller is a servlet that forwards requests to the View and models respectively.
If your application is a simple one you should still try to maintain the MVC (or model 2)structure as much as possible. Consider this good habit forming. You can find information on the MVC or Model 2 design patters here;
Here is SUN's representation of the MVC architecture:
Here is another good description of the MVC architecture:
Once you have mastered the MVC (model 2) architecture, try your hand at the STRUTS design pattern. I wouldnt try struts until you are firm in your knowledge of MVC.
STRUTS located here;

I hope this helps
[ August 13, 2002: Message edited by: Steven Kors ]

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
HI all,
Lot of thanks for all of urs response
I agree. Here's the link:
subject: Difference between JSP and SERVLET??
It's not a secret anymore!