I am new to java(learning JSF and other JAVA EE components) and have a very basic question.
Why do we need a Servlet when lot of the things can be done with Beans. What is there in servlet which cannot be done from a Bean or how is using Servlet better than Beans in a web based application.
With JSF, you’re basically already using a servlet, the
FacesServletwhich you’ve most likely already registered in theweb.xmlyourself in order to get JSF to run. It’s exactly that servlet which removes the need to write a bunch of servlets to perform repeated tasks such as gathering the request parameters, converting/validating them, updating javabean properties, invoking actions and navigating to the right view.In JSF you do not need to create additional servlets to perform those tasks. You just create and declare a managed bean as a controller which in turn holds a simple javabean class as model which get bound to UI components in the view.
But sometimes JSF is overkill or too hard whenever one has never learnt JSF before and just want two, three or four web pages with only a contact form. JSF has a relatively steep learning curve, which requires a solid understanding of HTTP servlets as well. With “plain vanilla” servlets and JSP it’s then easier to develop. But whenever the site grows out of its borders and you start copypasting/refactoring the common tasks, you’d be happy if you had chosen a MVC framework beforehand.