Download the following JAR files from the book's web site. These are in addition to the JAR files that were added from Chapter 4.
hibernate3
hibernate-annotations
hibernate-commons-annotations
ejb3-persistence
hibernate-validator
Right-click the Libraries folder in the NetBeans project.
Select Add Jar/Folder
Navigate to the directory that contains the downloaded JAR files.
Select all four JAR files and select Open.
Open the Source Packages folder.
Add a class named HelperBaseCh5 to the shared package.
Extend it from HelperBaseCh4.
Create a non-default constructor.
It should have a parameter that is a request object.
It should have a parameter that is a response object.
Call the super class's constructor.
Declare a public member variable named errorMap that is map
keyed by strings and contains strings.
Add a method named getErrors that returns the error map. JSPs
will use this to retrieve the error map.
Add a method named setErrors that has a parameter that is an
object.
Create a class validator for the parameter's class.
Fill an array of validation messages from the parameter.
Clear the error map variable.
Fill the error map with the information from the validation messages.
Create a boolean method named isValid that has a parameter that
is an object.
Call the setErrors method with the parameter.
Return true if there are no errors; otherwise, return
false.
Create a new package named ch5.requiredValidation.
Copy the controller class and the controller helper class from the
Enhanced tutorial for Chapter 4. Refactor as necessary.
Copy the edit, confirm and process pages from the Enhanced tutorial
for Chapter 4 into this package.
Copy the RequestData bean from the Data Bean tutorial
in Chapter 3. Rename the bean to RequestDataRequired.
Modify the bean.
Add the Pattern annotation to each accessor.
org.hibernate.validator.Pattern
The Pattern annotation is only for accessors that return a string.
Try to validate that the string contains at least one word of letters and
numbers. There could be more than one word.
Add the NotNull annotaion to each accessor.
org.hibernate.validator.NotNull
The NotNull annotation is only for accessors that return an
object. It won't work for accessors that return numbers.
Modify the controller helper.
Extend the class from HelperBaseCh5.
Change all bean references to the bean for this package.
Change the location of the JSPs to this package.
In the confirm method, if the data is valid, proceed to the confirm page;
otherwise, return to the edit page.
Modify the edit page.
Add error messages for each property.
The error message will appear when the data is invalid.
Edit the web.xml file.
Add a servlet definition for the controller.
Add a servlet mapping for the controller.
Do not create servlet definitions or servlet mappings for any of the other
files in the package.
Modify the index.jsp file.
Modify the page by adding a hypertext link to the controller.
Do not link to the edit page; all accesses should be to the controller.
Run the project.
The index.jsp page will appear in the browser.
Do not enter any data and hit the button.
Error messages will appear on the page.
Enter valid data into one of the fields and hit the button.
Only one error message will appear.
Enter valid data into both fields and hit the button.
You proceed to the confirm page.
Validate that all the JSP files contain valid HTML.
Be sure that the logger is working for this application.
Whenevere a log file is used. you will need to shut down Tomcat in NetBeans
before doing a clean and build.
Click the Services tab in the NetBeans project.
Open the Servers folder.
Right-click Apache Tomcat.
Click Stop.
Now you can do a clean and build.
Post
Open the Source Packages folder.
Create a new package named ch5.requiredPost.
Copy the controller class and the controller helper class from the
Required Validation tutorial. Refactor as necessary.
Copy the edit, confirm and process pages from the Required
Validation tutorial.
Modify the controller.
Add a doPost method.
Except for the name, the method has the same signature as doGet.
Create a controller helper object and call its doPost method.
Modify the controller helper.
Add an import for the bean from the Required Validation tutorial.
Change the location of the JSPs to this package.
Add a doPost method.
The method should have no parameters.
The method should throw IOException and
ServletException.
Copy the contents of the doGet method.
Modify the doGet method.
Do not read the old data from the session.
Always call the edit method, regardless of the button that is clicked.
Modify the JSPs.
Add a method attribute to each form.
Set the method to POST.
Edit the web.xml file.
Add a servlet definition for the controller.
Add a servlet mapping for the controller.
Modify the index.jsp file.
Modify the page by adding a hypertext link to the controller.
Do not link to the edit page; all accesses should be to the controller.
Run the project.
The index.jsp page will appear in the browser.
Enter data and click each button in the application.
Notice that the data does not appear in the query string in the URL.
Navigate to the confirm page and reload the page in the browser.
You will be asked if you want to send the data again. This is what happens
when you reload a page that uses POST.
Return to the edit page.
Place the mouse into the location box in the browser and hit enter.
A GET request has been initiated, so the old data is not read. There will
be no data in the input elements.
Validate that all the JSP files contain valid HTML.
Be sure that the logger is working for this application.
Whenevere a log file is used. you will need to shut down Tomcat in NetBeans
before doing a clean and build.
Click the Services tab in the NetBeans project.
Open the Servers folder.
Right-click Apache Tomcat.
Click Stop.
Now you can do a clean and build.
Data Persistence
Downlaod and unzip the two zip files from the book's web site.
hibernate zip
non-hibernate zip
Right-click the Libraries folder in the NetBeans project.
Select Add Jar/Folder
Navigate to the directory that contains the unzipped JAR files.
Select all four JAR files and select Open.
Do not add the folder or the zip file, you must add each JAR file separately.
Right-click the Libraries folder in the NetBeans project.
Select Properties.
There are duplicates entries for the following JAR files, since they were
added previously. Remove the duplicates.
log4j-1.2.11
commons collections
commons logging
commons beanutils
hibernate3
hibernate-annotations
hibernate-commons-annotations
hibernate-validator
ejb3 persistence
Open the Source Packages folder.
Add a class named HibernateHelper to the shared
package. Copy the contents from the book's web site.
Add a class named PersistentBase to the shared
package. Copy the contents from the book's site.
Add a class named WebappListener to the shared
package. Copy the contents from the book's site.
It closes the Hibernate factory.
It will closes the database drivers.
This is very important; it reduces memory leaks in Java.
Create a new package named ch5.persistentData.
Copy the controller class and the controller helper class from the
Post tutorial. Refactor as necessary.
Copy the edit, confirm and process pages from the Post tutorial.
Copy the bean from the Required Validation tutorial. Rename
it to RequestDataPersistent.
Modify the bean.
Add the class annotation that indicates that the class will have a separate
table in the database. Use the one from javax.persistence.
Extend the class from shared.PersistentBase. Alternatively,
you can add an id property, but don't do both.
Mark any properties that should not be saved in the database with the appropriate
annotation.
Modify the controller helper.
Change all bean references to the bean for this package.
Change the location of the JSPs to this package.
Add a public, static method named initHibernate that has a boolean
parameter.
Add a local variable of type java.util.Properties.
Set the properties for Hibernate. Copy all the properties from the book's
example and change the following.
Set the database dialect.
Set the server.
Set the port.
Set the database.
Set the username.
Set the password.
If the boolean parameter is true, then call
createTable from HibernateHelper. Pass the properites
and the class of the bean.
Call initSessionFactory from HibernateHelper. Pass
the properites and the class of the bean.
Modify the process method.
Validate that the data is still valid.
If the data is not valid,
Direct the user to an error page that explains that the session has expired.
You will need to add that page to the package.
If the data is valid,
Write the bean to the database.
Retrieve a list containing all the data from the database.
Place the list of data in the request for access by the JSPs.
It is important that this is placed in the request and not the session.
Modify the controller.Add an init method with no parameters.
Retrieve the initialisation parameter named create that will
be defined in web.xml.
Parse it as a boolean.
Call the static method initHibernate from the controller helper;
pass the boolean value.
Modify the process page.
Add a form that uses the GET method and goes to the edit page. Name the button
New.
Add the taglib for the JSTL.
Add a forEach loop that displays all the data from the database.
Use EL to retrieve the data using the name that was used to add the data
to the request in the process method.
Display each row on a separate line.
List all the data for each row, separated by commas.
Edit the web.xml file.
Add a listener tag.
Set the listener class to share.WebappListener.
This will make sure that Hibernate and the database drivers release resources
when the web application is removed from memory. It is essential for minimizing
memory leaks in Java.
Add a servlet definition for the controller.
Add an initialisation parameter named create.
Set the value to true when you want to create the table; otherwise,
leave it as false.
Add a servlet mapping for the controller.
Modify the index.jsp file.
Modify the page by adding a hypertext link to the controller.
Do not link to the edit page; all accesses should be to the controller.
Run the project.
The index.jsp page will appear in the browser.
Enter some data and navigate to the process page.
You will see a list of all the data that you have entered.
Click the Edit button.
Change the values and navigate to the process page.
You will see that the data for the last record was changed, instead of a
new record being added.
Click the New button.
Enter some new data and navigate to the process page.
You will see that a new record has been added to the database.
Be sure that the create parameter in the web.xml
file is set to false, so that your table will not lose all its
data when the servlet engine restarts.
Navigate to the confirm page.
In the browser, remove the cookie for JSESSIONID.
You should still be on the confirm page. Click process. You will be redirected
to the error page, since the session is no longer accessible and the data
is gone.
Validate that all the JSP files contain valid HTML.
Whenevere a log file is used. you will need to shut down Tomcat in NetBeans
before doing a clean and build.
Click the Services tab in the NetBeans project.
Open the Servers folder.
Right-click Apache Tomcat.
Click Stop.
Now you can do a clean and build.
Configuration File
Open the Source Packages folder.
Create a package named ch5.persistentData.configure.
Copy the controller from ch5.persistentData into this package.
Create a new controller helper.
Extend the class from ch5.persistentData.
Copy the constructor from the controller helper in the ch5.persistentData package.
Create an initHibernate method that has a boolean parameter named create. This has the same signature as the initHibernate in the ch5.peristentData package. Add the following statements to the method:
if (create) {
HibernateHelper.createTable(RequestDataPersistent.class);
}
HibernateHelper.initSessionFactory(RequestDataPersistent.class);
Create a doGet method. Call the doGet of the super class.
Create a doPost method. Call the doPost of the super class.
Create a file name hibernate.cfg.xml in the default package under source packages. If there is not a default package, then add the file to source packages directly. Add the contents of this link to the file.
Change the dialect to agree with your database server.
Change the driver to agree with your database server.
Change the connection url for your server.
Change the port for your server.
Change the username for connecting to your server.
Change the password for connecting to your server.