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

Servlets, Jsp

Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
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

Joined: Jan 07, 2004
Posts: 270
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
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

Joined: Jan 07, 2004
Posts: 270
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Jul 25, 2005
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets, Jsp