Using beans in servlets

I have a jsp page (index.jsp) with a form with two text names and a type password.

<form action="MyClass"> <input type="text" name="username" id="username" /> <input type="password" name="password" id="password" /> <input type="submit" /> </form> 

Upon presentation of the form, I run the servlet. I know that we can get the entered username and password values ​​using the request methods,

  request.getParameter("username"); request.getParameter("password"); 

But I do not want to use them, instead I want to store these values ​​in a bean called BeanClass, and I want to get the values ​​from the bean to sevlet. How can i get it?

+3
source share
2 answers

You must use the <jsp:useBean/> action to create an instance of BeanClass with a request or session scope in the JSP .

Example - EmpServlet.java

 package com.me; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EmpServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw=response.getWriter(); Emp emp=(Emp)request.getAttribute("emp"); pw.print(emp); } } 

Emp.java: Emp bean

 package com.me; public class Emp { private int age; private String name; public Emp() { name=""; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean valid() { return age!=0 && name.length()!=0; } @Override public String toString() { return "Emp{" + "age=" + age + ", name=" + name + '}'; } } 

emp.jsp (view)

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <jsp:useBean id="emp" class="com.me.Emp" scope="request"> <jsp:setProperty name="emp" property="*"/> </jsp:useBean> <c:if test="${emp.valid()}"> <jsp:forward page="emp"/> </c:if> <form method="post" action="emp.jsp"> <br/><input type="text" name="age"/> <br/><input type="text" name="name"/> <br/><input type="submit"/> </form> 

web.xml

 <servlet> <servlet-name>EmpServlet</servlet-name> <servlet-class>com.me.EmpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EmpServlet</servlet-name> <url-pattern>/emp</url-pattern> </servlet-mapping> 
+2
source

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() { // Do here your job. } // Add/generate getters and setters. } 

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.

+1
source

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


All Articles