This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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 ]
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.