Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use beans or servlets

 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
bharat nagpal
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic