aspose file tools*
The moose likes Servlets and the fly likes Why use a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why use a servlet" Watch "Why use a servlet" New topic
Author

Why use a servlet

Gary Guion
Greenhorn

Joined: Jun 21, 2001
Posts: 23
In the development of a web site if I had two choices; one use a servlet, jsp pages and some beans or two use all beans and jsp pages. Which one of the choices is best?
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
A servlet is Java code with embedded HTML. A JSP is HTML with embedded Java code. I've used both extensively, and I can tell you that servlets are good for encapsulating server-side business logic whereas JSP's are good for delivery of client-side HTML content. The reason for this is because a JSP is easy to update and maintain, as long as what you are updating is the HTML, not the Java code. Since java code is embedded in the JSP, it's difficult to read and can be a real bitch to maintain. JSP's are great for interacting with server-side components, like EJB, and getting the data they need to format and display to the client in HTML. I've worked with HTML and Javascript develelopers who were not even programmers and they were able to maintain the HTML and Javascript in JSP's and get around the Java code! Try doing that with a servlet!
Java code in JSP's should be kept to a minumum, and you should avoid placing business logic in JSP as well. I know that JSP's are server-side objects like servlets, but I like to think of them as front-end content delivery. If you find yourself putting business logic in JSP's, then perhaps you should consider using a servlet instead, or just a bean.
Servlets are great to chain together, where each is performing a high cohesive function. This promotes maintainability and reusability, of course.
Hope it helps!
SAF

Gary Guion
Greenhorn

Joined: Jun 21, 2001
Posts: 23
That is a lot of help. I still am not completely sure about one thing... is it best then to use beans for all business and jsp for all presentation and then go without the servlets? I am trying to decide if I should use servlets at all and I don't think that I need them if I am using beans and jsps.
Thanks,
Gary
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
That's true, you may not need servlets. I worked on a 3-tier project last year that involved JSP for the presentation layer, EJB for the business logic layer, and Oracle in the back end. THe JSP's communicated with the EJB's for data and delivered the content to the client's browser. Toward the end of the project, I noticed that the JSP's were begininning to get really complicated some of the developers were placing business logic in them. TO make a long story short, make sure you dont do this because these pages did'nt last in production too long! They were impossible to maintain.
Remember, JSP ----> Presentation! and that's it, make sure you keep your business logic out of them!
SAF
Gary Guion
Greenhorn

Joined: Jun 21, 2001
Posts: 23
Thanks for the response.
Cordially,
Gary Guion
Rehan Malik
Ranch Hand

Joined: Jul 09, 2001
Posts: 76
Originally posted by Gary Guion:
In the development of a web site if I had two choices; one use a servlet, jsp pages and some beans or two use all beans and jsp pages. Which one of the choices is best?


Servlets can be used to perform backend information such as verifying passwords, logging users in etc. Those are just examples off the top of my head.
I've used servlets for making DB connections while using javabeans for file I/O to read a few configuration settings that had to be saved.
It depends on which way you want to go. I've ONLY used servlets for the business logic, don't use it for anything more. JSP is also for JUST the presentation. If you try to use it for more than that, then well, good luck maintaining the site =)

Rehan Malik
Gary Guion
Greenhorn

Joined: Jun 21, 2001
Posts: 23
Thanks again. My biggest dilemma is I want to use jsp's for presentation and beans for logic and my partner/developer says we should use servlets in some cases. I don't see why we need any servlets.
Thanks,
Gary
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why use a servlet