Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets, Jsp

 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one tell me, when is necessity for using jsp in developing website. and when to use servlet. and when to use both jsp and servlet. what are the uses of using both JSP and servlet.

And when to use jsp, servlet and ejb all 3. Can any body explain me clearly with example.

Thanks
Arathi
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use jsp files for the graphic only, i.e. forms. From the jsp files I post the data to the Servlets, which do the insert, update and delete to the database. All other database queries and dataobjects I put in ordinary java classes for reuse.

In Jsp files it is easy to write your HTML code, javascript and java code is just written with the use of <% your java code %>.

A servlet is a normal java class file where you can request the data posted to the servlet. Here you just write java code like in ordinary classes, but to write the html code is a bit slow, because you have to make an out.println("<html><body>Hello servlet</body><html>"); to print out your graphic.

In the servlet you can also use the init(), service(), destroy()...

See also:
http://www.devx.com/tips/Tip/25217
[ July 30, 2005: Message edited by: Jeppe Fjord ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up "Model, View, Controller" or MVC.
In short, it's an architecture that combines both with JavaBeans to create a really clean separation of concerns within your webapp.
 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did understand the concept of JSP and servlets. But tell me we can also fetch the data from database using jsp, then why do we require servlets. I know jsp is document centric. what is the disadvantage of using jsp to access database.

Thanks
Arathi
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with putting everything into a jsp file is that it will gonna be a mess! Servlets are made to handle the communication from the HTTP protocol. In the servlet you have the life cycle methods to handle the requests (init, destroy...). You can control your servlets within your web.xml file in different ways, i.e. give them parameters, load on startup, security constraints etc... Because the servlet is running on the server side, it does not depend on browser compatibility.

You usually split up your application into two portions:

The business logic (content generation), which governs the relationship between input, processing, and output

The presentation logic (content presentation, or graphic design rules), which determines how information is presented to the user

Using this rationale, you may choose to have business logic handled by Java beans, the presentation logic handled by JavaServer Pages (JSP) or HTML files, and the HTTP protocol handled by a servlet.

If your are working on bigger apps then you will soon see the benefits of doing the above, but on a very small and simpe application maybe you won�t see it.
[ July 31, 2005: Message edited by: Jeppe Fjord ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Splitting out logic to separate classes has the VERY great advantage that - if you plan correctly - most parts of your application can be tested outside the servlet container environment. The servlet - JSP environment introduces so many complexities that it really helps if you are sure that your logic components work correctly.
Bill
 
Machavarapu Srinivasa Baba
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arathi Raj:
Hi,

Can any one tell me, when is necessity for using jsp in developing website. and when to use servlet. and when to use both jsp and servlet. what are the uses of using both JSP and servlet.

And when to use jsp, servlet and ejb all 3. Can any body explain me clearly with example.

Thanks
Arathi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic