Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why use a servlet

 
Gary Guion
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
Cordially,
Gary Guion
 
Rehan Malik
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 =)
 
Gary Guion
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic