Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets vs JSP

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everybody,
What are advantages and disadvantages of JSP over Servlets?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Peter,
Which is more convenient and maintenable? I think JSP.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic