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: 263
posted
0
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()...
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.
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: 263
posted
0
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: 12271
1
posted
0
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
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.