This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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()...
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.
Joined: Jan 07, 2004
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 ]
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