HTML and CSS Reference
In-Depth Information
@Stateless annotation defines the UserManager class as a stateless session EJB. @PersistenceContext
annotation is used for injecting a container managed entity manager instance. Using the injected entity manager
instance, we will be able to perform the database operations. In the UserManager EJB, there are two main methods:
1. getUser() method, which retrieves the user using the user name and password from the
database. It returns null if the user does not exist.
2. registerUser() method, which does the following:
UserExistsException .
If the user ID already exists, it throws
Listing 10-20 shows UserExistsException class.
If the user ID does not exist, then the user is saved in the database.
Listing 10-20. UserExistsException Class
public class UserExistsException extends Exception {
UserExistsException is a simple custom exception which extends Exception class.
The weather application is developed under GlassFish version 4.0. Listing 10-21 shows glassfish-resources.xml
which defines the application data source.
Listing 10-21. Weather Application glassfish-resources.xml File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-// GlassFish Application Server 3.1 Resource
Definitions//EN" " " >
<jdbc-connection-pool ...>
<property name="serverName" value="localhost"/>
<property name="portNumber" value="1527"/>
<property name="databaseName" value="weatherDB"/>
<property name="User" value="weather"/>
<property name="Password" value="password"/>
<property name="URL" value="jdbc:derby://localhost:1527/weatherDB"/>
<property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
<jdbc-resource enabled="true" jndi-name="jdbc/weatherDB" object-type="user"
In order to add the defined resources of glassfish-resources.xml in your GlassFish 4, you need to start your
GlassFish server by running the following command from the bin directory of the server.
> asadmin start-domain
After the server starts, you can use asadmin add-resources command as follows in order to add the defined
resources in your server.
> asadmin add-resources <<full path>>/glassfish-resources.xml
Search WWH ::

Custom Search