In this chapter we will deploy our 3T application on the GlassFish 4 server. A successful deployment will require several new configuration files as well as updates to existing files. You will already be familiar with some of these files from the testing configuration defined in Chapter 5, Testing the DAO Layer with Spring and JUnit, but a few new files specific to GlassFish will be introduced.
We will also be configuring the GlassFish server to run independent of the NetBeans IDE. Enterprise environments will usually have many GlassFish server instances running on different hosts. Understanding basic GlassFish configuration is an important skill and we will cover connection pool configuration in detail.
At the end of this chapter you will be able to see dynamic HTTP responses based on the URLs you have mapped so carefully in Chapter 7, The Web Request Handling Layer.
The web application configuration requires several new files that will need to be added to the WEB-INF
directory, as shown in the following screenshot. Create these files now:
Note that the beans.xml
file was created by NetBeans but is not required for our configuration. Let's now look at each of these files in detail.
The applicationContext.xml
file configures the Spring container and is very similar to the testingContext.xml
file we created in Chapter 5, Testing the DAO Layer with Spring and JUnit. The contents of the file are as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <bean id="loadTimeWeaver" class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver" /> <bean id="entityManagerFactory" p:persistenceUnitName="tttPU" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" /> <!-- Transaction manager for JTA --> <tx:jta-transaction-manager /> <!-- enable the configuration of transactional behavior based on annotations --> <tx:annotation-driven /> <!-- checks for @Autowired beans --> <context:annotation-config/> <!-- Scan for Repository/Service annotations --> <context:component-scan base-package="com.gieman.tttracker.dao"/> <context:component-scan base-package="com.gieman.tttracker.service"/> </beans>
This file is used by Spring to initialize and configure the JPA EntityManagerFactory
and TransactionManager
DAO and Service layer objects. Comparing the applicationContext.xml
file with the testingContext.xml
file identifies the key differences between a simple Java container and a Java EE container provided by an enterprise application server:
applicationContext.xml
file. The JNDI configuration setting in the persistence.xml
file is defined later in this chapter.<tx:jta-transaction-manager />
and <tx:annotation-driven />
definitions are all that is required to configure transactional behavior within the Spring container.When the applicationContext.xml
file is loaded by the Spring container, the MVC configuration classes defined in Chapter 7, The Web Request Handling Layer, are automatically discovered by classpath scanning and are loaded to configure the web application components.
The web.xml
web application deployment descriptor file represents the configuration of a Java web application. It is used to configure the servlet container and map URLs to each configured servlet. Each Java web application must have a web.xml
in the WEB-INF
directory of the web application root.
The 3T web application requires the following web.xml
definition:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <session-config> <session-timeout>30</session-timeout> <cookie-config> <name>JSESSIONID_3T</name> </cookie-config> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Some key points are as follows:
context-param
element defining the contextConfigLocation
value is optional if the Spring configuration file is named applicationContext.xml
(this is the expected default filename if not supplied). However, we always include this property for completeness. It defines the location of the main Spring configuration file.org.springframework.web.context.ContextLoaderListener
is used by Spring to initialize loading of the application context. It is the entry point to boot the Spring container and attempts to load the contextConfigLocation
file. An exception is thrown if the file cannot be resolved or is invalid.session-config
properties define the session timeout (30 minutes of inactivity) and the session cookie name.welcome-file-list
identifies the file that will be served by GlassFish, if not specified explicitly in the URL.The
glassfish-web.xml
file configures GlassFish with additional web application properties specific to the GlassFish server:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"> <glassfish-web-app> <context-root>/</context-root> </glassfish-web-app>
The context-root
property identifies the web application's server path for deployment. We will deploy the 3T application to the context root of the server. This means that 3T request handlers can be accessed directly from the root of the web application as in the following example:
/ttt/company/findAll.json
Changing the context-root
property to /mylocation
, for example, will require a URL of the following format:
/mylocation/ttt/company/findAll.json
3.15.231.194