Essentially, you're looking for an MVC infrastructure like JSF or Spring MVC . With JSF, it will look something like this:
<h:form> <h:inputText value="#{bean.username}" required="true" /> <h:inputSecret value="#{bean.password}" required="true" /> <h:commandButton value="submit" action="#{bean.submit}" /> <h:messages /> </h:form>
with
@ManagedBean @RequestScoped public class Bean { private String username; private String password; public void submit() {
It's all. No servlet needed.
If you really want to do this at the low-level servlet level, you will need to populate the bean yourself. This could be due to Apache Commons BeanUtils to save boilerplate code.
Bean bean = new Bean(); BeanUtils.populate(bean, request.getParameterMap()); request.setAttribute("bean", bean);
<jsp:useBean> does not allow you to use the MVC approach, it is rather MV. You must mix the conversion / validation into a model and manage the request / response within the view, the tasks that the controller must perform. MVC frameworks offer you a controller that takes all these nasty template tasks out of your hands.
source share