Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between servlet and jsp

 
sridhar rao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between servlet and jsp..

if its possible please give more information..
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://java.sun.com/developer/onlineTraining/J2EE/Intro2/j2ee.html
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-edit-

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