File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes When to use beans or servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "When to use beans or servlets" Watch "When to use beans or servlets" New topic

When to use beans or servlets

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
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

Joined: Oct 26, 2002
Posts: 76
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).
[ July 17, 2004: Message edited by: bharat nagpal ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

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.

[Asking smart questions] [About Bear] [Books by Bear]
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

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.

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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:
subject: When to use beans or servlets
It's not a secret anymore!