• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

When to use beans or servlets

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it difficult to decide when to use beans and servlets.
I have made a login page which when submitted can go to a servlet which can verify the user name and password and accordingly allow or disallow the user.
But instead of this I can also submit the form to a JSP page which can check the user name and password in the bean.

Similarly when the user submits a forms to be inserted into the database, values can be send to the servlet where they can be inserted in the database. Also they can be send to a JSP page which can set the properties to a bean which can do the insertion.

Please tell me when it is appropriate to use a servlet and when a bean.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically JSP+Beans serve same purpose as Servlets. Servlets tend to mix presentation with business logic, while using JSP with Java beans allows you to seperate your presentation logic from business logic. So whenever there is presentation involved always use JSP with beans. Let's say tomorrow you need to change HTML to UML...only your jsp files will change and also your jsp can be easily maintained by designers. Servlets are generally used when there is no presentation invloved. e.g as controller(in MVC).
HTH
[ July 17, 2004: Message edited by: bharat nagpal ]
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an easy one: never submit a form to a JSP. Period. JSPs should be restricted to creation of the view; all processing should be done in a servlet controller.
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your question is whether to use JSP or Servlet, then you can use either one really, because the JSP basically gets compiled into a Servlet, but the 'purer' approach is to use Servlet for receiving form input and JSP to show it.

One advantage JSP has over Servlet as recepient a of form is that it can automatically set the properties of the JavaBean used based on the input parameters, so it saves you some work of explicitly calling setXXX methods.

Whichever way you go, it is best to keep your business logic code in the Bean, it is easier to test, debug, and reuse. This may not be apparant if the amount of code is very little, but as the beans get bigger and start to do more complex tasks, you will see the difference.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no beans OR servlets, there's beans AND servlets.

Servlet populates the beans, JSP displays the content and submits the next request to servlet which populates more beans, etc. etc.
 
Hey cool! They got a blimp! But I have a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic