|
With the DB Visual ARCHITECT (DB-VA) you can develop quality Java Enterprise Web Application much faster, better and cheaper. All DB-VA generated code, configuration file and persistent layer library are deployable to most application servers. DB-VA generates all Java code for accessing database. You do not need to write SQL to insert, query, update or delete the record. All code you need to program is plain Java code (e.g. OrderDAO.save(myOrder);). In this chapter we will use a simple "School System" application to show you how to generate Java code, configure your web application, creating, querying, updating and deleting objects. Again you do not need to write a single SQL statement for all the above operations. In the final section we will discuss some issues about the HTTP Session.
![]() |
|---|
| Figure 3.1 - Java Enterprise Web Application |
The architecture of Java Enterprise Web Application with DB-VA Persistent Layer
In this chapter:
You will develop a School System.
The School System provides the following functions:
Required Software:
Please open the SchoolSystem.vpp project file in the Chapter 3 School System.zip file. The project file contains the following diagrams. For the details about how to draw class diagram and entity relationship diagram, please see the Designer's Guide.
Class Diagram of School System:
![]() |
|---|
| Figure 3.2 - Class Diagram of School System |
Entity Relationship Diagram of School System:
![]() |
|---|
| Figure 3.3 - ERD of School System |
Each Java Web Application has Web Application Deployment Descriptor (web.xml) to configure the deployment and startup of the web application. Please select "Web Application Deployment Descriptors (web.xml)" option to generate the web.xml for your web application.
![]() |
|---|
| Figure 3.4 - To generate code |
Code tab:
For Generate, select Code and Database to generate code and create database option.
For Language, select Java language.
For example:
For Deploy To, select JBoss Application Server, DB-VA will help you to select the optimize option Jar to create orm.jar (persistent library).
For Association Handling, select Smart.
For Persistent API, select Factory Class.
For Sample, check Sample and Servlet Sample.
Select Web Application Deployment Descriptors (web.xml) to generate web.xml with filter configure.
![]() |
|---|
| Figure 3.5 - Generate code configure for generate web application |
Database tab:
You can reference Chapter 1 to select export the database schema and configure the default database of JDBC connection.
![]() |
|---|
| Figure 3.6 - Creatae a new Eclipse project |
For Project name, Enter "School System"
Select Create project from existing source. In Directory, select the JBOSS_DEPLOY_FOLDER\schoolsystem.war folder.
Click Next >.
![]() |
|---|
| Figure 3.7 - Select directory of the generated code |
![]() |
|---|
| Figure 3.8 - Select the output path and src path |
![]() |
|---|
| Figure 3.9 - Open the project properties |
![]() |
|---|
| Figure 3.10 - Add an external Jar |
![]() |
|---|
| Figure 3.11 - Select the external Jar |
![]() |
|---|
| Figure 3.12 - To move the file |
![]() |
|---|
| Figure 3.13 - Select the destination directory |
<display-name>SchoolSystem</display-name>
<description>SchoolSystem</description>
<filter><filter-name>ORMFilter</filter-name></filter>
<filter-class>school.SchoolSystemFilter</filter-class>
<filter-mapping><filter-name>ORMFilter</filter-name></filter-mapping>
<url-pattern>/*</url-pattern>
The school system provides a separate register page for teacher and student to enter their information. The register method of teacher and student method is the same, so we will demonstrate how to create the teacher and save to database.
<head></html><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head>
<title>Teacher Register</title>
<body><h2>Teacher Register</h2></body>
<hr>
<form action="servlet/createUser" method="post"><table border="0"></form><tr><td><strong>User ID : </strong></td><td><input name="loginID" type="text" id="loginID"/></td></tr></table>
<tr><td><strong>Teacher Full Name : </strong></td><td><input name="name" type="text" id="name"/></td></tr>
<tr><td><strong>Password : </strong></td><td><input name="password" type="text" id="password"/></td></tr>
<tr><td><strong>Email : </strong></td><td><input name="email" type="text" id="email"/></td></tr>
<tr><td></td><td><input type="submit" name="Submit" value="Submit"></td></tr>
<input name="userType" type="hidden" value="teacher"/>
Source File : School System\teacherreg.html
![]() |
|---|
| Figure 3.14 - The teacherreg.html view in browser |
Source File : School System\WEB-INF\src\util\CreateUser.java
Source File : School System\WEB-INF\src\util\CreateUser.java
Source File : School System\WEB-INF\src\util\CreateUser.java
<servlet-name>CreateUser</servlet-name></servlet>
<servlet-class>util.CreateUser</servlet-class>
<servlet-name>CreateUser</servlet-name></servlet-mapping>
<url-pattern>/servlet/createUser</url-pattern>
Source File : School System\WEB-INF\web.xml
![]() |
|---|
| Figure 3.15 - The successful message |
![]() |
|---|
| Figure 3.16 - The record is added to database |
You can retrieve the record in database as object. For example, you need to create the login function for the School System. You will require the user to input the User ID and password to login, the system retrieve the User object from the user id and compare the password to validate the user.
school.User lUser = (school.User)request.getSession().getAttribute("user");%>
if (lUser == null){
} else if (lUser instanceof school.Student){%>response.sendRedirect("studentPage.jsp");} else if (lUser instanceof school.Teacher){response.sendRedirect("teacherPage.jsp");}
Source File : School System\login.jsp
![]() |
|---|
| Figure 3.17 - The login.jsp view in browser |
request.getSession().setAttribute("user", lUser);}
if (lUser instanceof Student) {response.sendRedirect("../studentPage.jsp");} else {response.sendRedirect("../teacherPage.jsp");}
Source File : School System\WEB-INF\src\util\Login.java
You can modify the teacher information and update the record in database. You get the User object from the session and set the new values for the User object, finally call save() method to update the record in database.
![]() |
|---|
| Figure 3.18 - The teacherPage.jsp |
boolean isStudent = false;%>
Object lUserObj = request.getSession().getAttribute("user");
if (lUserObj == null){response.sendRedirect("login.jsp");} else {school.User lUser = (school.User)lUserObj;
Source File : School System\modifyUser.jsp
![]() |
![]() |
|---|---|
| Figure 3.19 - The password of user changed | |
PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();}
boolean lSuccess = false;
try {String lUserName = request.getParameter("name");} catch (Exception e) {
String lPassword = request.getParameter("password");
if (lUserName != null && lUserName.length() > 0 && lPassword != null && lPassword.length() > 0) {User lUser = (User) aUserObj;} else {
lUser.setName(lUserName);
lUser.setPassword(lPassword);
if (lUser instanceof Teacher) {((Teacher) lUser).setEmail(request.getParameter("email"));}
lUser.save();
lSuccess = true;lSuccess = false;}
t.commit();e.printStackTrace();}
t.rollback();
return lSuccess;
Source File : School System\WEB-INF\src\util\ModifyUser.java
Teacher can create course for students for registration and they can cancel the course in the system. They only need to click cancel hyperlink of the course then the course information will be deleted in the database and all its relationship with registered students will be removed.
![]() |
|---|
| Figure 3.20 - Create course |
![]() |
|---|
| Figure 3.21 - Student Page |
![]() |
|---|
|
private void dropCourse(HttpServletRequest request)throws PersistentException{ String lID = request.getParameter("courseID"); PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction(); try{ Course lCourse = CourseFactory.loadCourseByORMID(Integer.parseInt(lID));
...
Source File : School System\WEB-INF\src\util\DeleteCourse.java |
|
lCourse.deleteAndDissociate(); t.commit(); }catch(Exception e){ e.printStackTrace(); t.rollback(); } }
Source File : School System\WEB-INF\src\util\DeleteCourse.java |
In School System, you have put the User persistent object in HttpSession to determine the user is login or not.
Set User Object to Session:
| request.getSession().setAttribute("user", lUser);
Get User Object From Session: Object lUserObj = request.getSession().getAttribute("user"); |
|
|
|||||||