Servlet vs Beans

I am new to java (learning JSF and other JAVA EE components) and ask a very simple question.

Why do we need a servlet when much can be done with Beans. What is in the servlet that cannot be done using Bean or how it uses Servlet better than Beans in a web application.

+4
source share
2 answers

In JSF, you already use the servlet, FacesServlet , which you most likely already registered in web.xml yourself to run JSF. This is the servlet that eliminates the need to write a bunch of servlets to perform repetitive tasks, such as collecting request parameters, converting / checking them, updating javabean properties, invoking actions, and switching to the correct view.

In JSF, you do not need to create additional servlets to perform these tasks. You simply create and declare a managed bean as a controller, which in turn contains a simple javabean class as a model that is bound to the user interface components in the view.

But sometimes the JSF is too overkill or too zealous when someone has never recognized the JSF before and just wants two, three or four web pages to have only a contact form. JSF has a relatively steep learning curve that requires a clear understanding of HTTP servlets. With "plain vanilla" servlets and JSP, it is easier to develop. But whenever a site grows out of its borders and you start copying / reorganizing common tasks, you would be happy if you chose the MVC structure in advance.

+7
source

Beans are used to represent your data. Servlets should be used to control your process.

In the MVC (Model, View, Controller) Beans template, there will be your model, which is data-oriented and represents your data, acts as domain objects or simple data structures.

Servlets are a controller that invokes the correct model and sends it in the correct order. They can be used to receive user queries and translate them into the correct input for models that will work.

I know the answer is very high, but try reading on the MVC template, you will get an idea better.

+4
source

Source: https://habr.com/ru/post/1415836/


All Articles