JSP vs. Servlets

S

Sathyaish

What are the pros and cons of using servlets instead of JSPs? As I
understand from my sparse reading of the topic so far, they're both
means of programming against the HTTP application model (i.e, request,
session, response, HTTP Runtime etc.).
 
L

Lew


Broadly speaking, JSPs are for the layout, or "view" aspect of an application,
and servlets are for the context and navigation, or "controller" aspect of an
application.

As the referenced tip suggests, JSPs look like HTML and servlets look like
Java. It is more natural in a JSP to concentrate on artifacts like drop-down
lists, submit buttons, headlines and the like. It is more natural in a servlet
to concentrate on things like parsing a request for its parameters, forwarding
the parameters to business and data logic, managing resource services,
coordinating processes and forwarding the view to the next JSP.

Both aspects of an application are required. Ideally, they will almost fully
separate concerns. You should nearly never have scriptlet (Java code) in a JSP
source file, nor response.out writes in a servlet .java source file.

Check around for the "model-view-controller" (MVC) application model, what Sun
calls the "Model 2 Architecture". It applies to Swing apps and other domains
besides Web apps. It ties in with service architectures and portals, even.

- Lew
 
D

Daniel Pitts

What are the pros and cons of using servlets instead of JSPs? As I
understand from my sparse reading of the topic so far, they're both
means of programming against the HTTP application model (i.e, request,
session, response, HTTP Runtime etc.).


A JSP is a different way to create a Servlet.
The focus of JSPs are presentation. JSPs are great for creating HTML
from a template.
Pure servlets are great for business logic.

A common approach is to have a Servlet that handles the business logic
(I.E. a Controller), loads data to be rendered (I.E. Model) and passes
that data to a JSP to be rendered (I.E. View)

There are plenty of libraries that help you do this.
Where I work we tend to use Spring Framework with Tiles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top