It's not a secret anymore!
The moose likes Servlets and the fly likes Difference between servlet and jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference between servlet and jsp" Watch "Difference between servlet and jsp" New topic

Difference between servlet and jsp

sridhar rao

Joined: Sep 25, 2005
Posts: 7
what is the difference between servlet and jsp..

if its possible please give more information..
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I know some of the following:

1] Servlet is mainly used for business logic, and JSP is mainly used for presentation.

2] In Servlet, you put HTML inside Java and in JSP you put Java inside HTML.

Some additional informations:

JSP ultimately converted into servlet before execution.
[ September 27, 2005: Message edited by: rathi ji ]
Keith Pitty
Ranch Hand

Joined: Sep 22, 2005
Posts: 32
A servlet is a special class that is instantiated by the web container to handle HTTP requests. Typically it delegates to JavaBeans or EJBs to perform business logic before transferring control to a JSP.

A JSP is a web page that includes some dynamic content (e.g. JSP scriptlets, JSP expressions, JSP tags) and is used to present a view to the user. At execution time it actually runs as a servlet (it is compiled into a servlet).

For more details you may be interested in exploring lessons 1 and 6 at:
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
I think it is not good practice to put the business logic in the servlet. We shouldn't assume that our application will be only accessed by the web. It is always better to place the business logic in the Model(Plain Old Java Code). Servlet can be used as a controller between the Java coding(Model) and JSP (or) Html (View - Presentation)

Whether my statement is correct??
Keith Pitty
Ranch Hand

Joined: Sep 22, 2005
Posts: 32
Yes, the Model View Controller (MVC) pattern is a widely accepted good practice for separating concerns in a J2EE web application. JavaBeans or POJOs (or EJBs) are used for the Model, JSPs for the View and Servlets for the Controller.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I agree with Mark. Use POJO's and EJB's for business logic. Your application can be accessed by mobile phones, PDA's, browser, fat clients etc.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A servlet looks like Java with some statements that write HTML (maybe).

An JSP looks like HTML with some Java script (maybe).

So a servlet is better suited to Java-like stuff: logic, complex processing, nifty OO models. A JSP is better suited to presentation stuff: HTML, JavaScript, etc.

A common model is for incoming GET and POST requests to hit a servlet which calls other Java classes that handle any inbound data and generate any outbound data. When all the Java work is done, the servlet forwards to a JSP that prepares the response page.

Under the covers the container compiles a JSP into a really ugly servlet, so the two can do most of the same things. But it's good to have a clear idea of what their roles are and how you want them to be different. Consider coding rules like no HTML in servlets and no Java in JSPs.

For more details, google and read up on MVC-2, front controller or Struts architecture.


Wow, a bunch of people said similar things while I was away reading my mail!
[ September 27, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Difference between servlet and jsp
It's not a secret anymore!