aspose file tools*
The moose likes Servlets and the fly likes Servlets vs  JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets vs  JSP" Watch "Servlets vs  JSP" New topic
Author

Servlets vs JSP

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi everybody,
What are advantages and disadvantages of JSP over Servlets?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by amit sanghai:

Hi everybody,
What are advantages and disadvantages of JSP over Servlets?

JSP is just another way of writing a servlet. In a plain servlet, you write Java code with embedded markup. In a JSP, you write markup with embedded Java code. Hence, a servlet is a better fit if your problem mainly involves logic while a JSP is a better fit if your problem mainly involves presentation (markup).
- Peter
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
Hi Peter,
I thought JSP was better in the sense that it seperates presentation(HTML) from Java code which you can write in Beans or some other way.

Servlets is better in the sense that JSP is first converted into a servlet so Servlets are faster than JSP.
Am I true???
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by amit sanghai:
I thought JSP was better in the sense that it seperates presentation(HTML) from Java code which you can write in Beans or some other way.

Nothing stops you from separating presentation and logic in a servlet. Only, the presentation bit then really wants to be a JSP (to quote my own words, it would "mainly involve presentation").

Servlets is better in the sense that JSP is first converted into a servlet so Servlets are faster than JSP.

In most cases, not significantly so. Convenience and maintainability are the most important considerations here.
HTH
- Peter
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Ref note at the bottom of page 11 of PROFESSIONAL JSP by various writers. quote " for each additional request of the JSP page thereafter, there is no delay because the request goes to the servlet byte code already in memory." unquote
hence, on the first "hit" a jsp page is translated into a java class, which is then compiled into a servlet.
Zaeem
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi Peter,
Which is more convenient and maintenable? I think JSP.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by amit sanghai:

Which is more convenient and maintenable? I think JSP.

Amit,
My answer would be: "it depends". The applications I do tend to use an MVC architecture. The model tends to be a bean, the view a JSP, and the controller a servlet.
If there's no presentation in your code, a JSP is generally inappropriate. Think not only of controllers, but also code that handles file uploads, pulls images from a database (for inclusion in an <img> tag), etc.
- Peter
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
as far as the conversion of jsp to servlets goes, the jsp is converted to servlet only once (initially) subsequent requests to the webserver do not create the servlet. So the jsp is slow only once 9but that can be countered and is always as some testing takes place before a serious client can access the servlet.
I disagree that servlets are faster than JSP's.
sagar
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi Peter,
I just wanted to ask who does the handling of file uploads, pulling of images from a database (for inclusion in an <img> tag)?
JSP or Bean or Servlet?
How does the MVC architecture work differently than the simple model architecture?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The JSP should know nothing about a database so that should be handled by a bean.
The key issue with JSPs is that it is for separation of logic from presentation. A poor programming style can lead to a mess however since it is easy enough to imbed a few hundred lines of Java code inside a JSP.
We also use an MVC architecture. The request comes into a servlet which packages the request and passes it to a bean for business logic processing and database lookups. The solution is then passed back to the servlet. The servlet then invokes the appropriate JSP depending upon the result supplied to it by the business login bean.
A JSP should know nothing about business logic or databases. It should only know how to take data and put it on the screen. The servlet should know nothing about business logic or databases or presentation. It should only know how to strip data from the screen and pass it to a bean for processing. The bean should know nothing about presentation but should know everything about business logic. If it needs data from a database, it should invoke a different bean that knows how to read data from the database. If this sounds a little like an EJB environment, then you have been paying attention. Even without an EJB server, the EJB layout is still the best way to separate functionality into reusable classes.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets vs JSP
 
Similar Threads
how i can exclude a url pattern from a filter ?
Servlet and JSP Book
Reply me Soon
Favourite "Head First" girl?
Doubt on Protocol & Port number