The integrity of most, if not all, applications depend on the correctness of its data. The Java API for JavaBeans Validation provides a means to validate data through the use of annotations.
This recipe explains this technique by introducing a driver's license entity. The next set of recipes illustrates how to specify constraints on several different types of fields. The last recipe pulls these techniques together and illustrates the use of the javax.validation.Validator
class to determine if the constraints were met.
First we will create an entity representing a driver's license and its supporting classes. Once this is working we will enhance the entity to incorporate validation annotations.
Create a Java EE application named ValidationApplication
. In the EJB module add a package called packt
and an entity called LicenseBean
. In addition, add an AbstractFacade
and LicenseBeanFacade
as illustrated in the Creating an Entity Facade recipe. In the WAR module add a package called servlet
. To this package add a servlet called LicenseServlet
.
Create an entity representing a driver's license called LicenseBean
. Its attributes include:
We will use these fields to demonstrate how validation can be performed.
@Entity public class LicenseBean implements Serializable { private String name; @Temporal(javax.persistence.TemporalType.DATE) private Date dateOfBirth; private String restrictions; private int monthsToExpire; public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { persistent propertiesvalidatingthis.dateOfBirth = dateOfBirth; } public int getMonthsToExpire() { return monthsToExpire; } public void setMonthsToExpire(int monthsToExpire) { this.monthsToExpire = monthsToExpire; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isResident() { return resident; } public void setResident(boolean resident) { this.resident = resident; } public String getRestrictions() { return restrictions; } public void setRestrictions(String restrictions) { this.restrictions = restrictions; } private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; public Long getId() { return id; } ... }
We will use the AbstractFacade
class developed in the Creating an entity facade recipe and derive the LicenseBeanFacade
class from it.
@Stateless public class LicenseBeanFacade extends AbstractFacade<LicenseBean> { @PersistenceContext(unitName = "ValidationApplication-ejbPU") private EntityManager em; protected EntityManager getEntityManager() { return em; } public LicenseBeanFacade() { super(LicenseBean.class); } }
The LicenseServlet
demonstrates the use of these classes.
public class LicenseServlet extends HttpServlet { @EJB LicenseBeanFacade licenseBeanFacade; LicenseBean license; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { license = new LicenseBean(); license.setName("Pax Maxwell"); Calendar calendar = Calendar.getInstance(); calendar.set(1981, 4, 18); license.setDateOfBirth(calendar.getTime()); license.setMonthsToExpire(24); license.setResident(true); license.setRestrictions("C6"); licenseBeanFacade.create(license); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet LicenseServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Name: " +license.getName() + " - License ID: " + license.getId() + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } }
Execute the application using the URL shown in the following screenshot:
This recipe sets up the LicenseBean
and LicenseServlet
for use in this chapter's remaining recipes. The LicenseBean
maintains information regarding a license. Notice the use of the @Temporal annotation in the LicenseBean
. When a time-based field, such as the java.util.Date
class is used, this annotation is required.
In the LicenseServlet
, a license was created and then persisted using the LicenseBeanFacade
instance. In later recipes we will modify the LicenseBean
and LicenseServlet
to demonstrate various validation techniques.
18.190.239.166