This article describes step by step how to configure
Tomcat (currently version 6.0.26) to work with different Java EE 6 components.
You can skip reading this article and get a
customized Tomcat and
sample application right away .
Tomcat
Why Tomcat and not
Glassfish V3 ? It is important for me to use Tomcat because it is compact, fast-running, and easy to cluster into a cluster running Apache Httpd. V3
is not yet able to work in a cluster.
Installation
- Install JDK 6 .
- Set JAVA_HOME environment variable
- Download the apache-tomcat-6.0.26.zip archive and unzip it to the current folder. The apache-tomcat-6.0.26 folder will be created.
For start:> bin/startup.(bat|sh) - откроет новое окно или
> bin/catalina.(bat|sh) run - запустит в новом окне.
To increase memory and debugging capabilities, you can specify the JAVA_OPTS environment variable before running catalina.bat:
set JAVA_OPTS=-Xmx512m -XX:MaxPermSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,address=1025,server=y,suspend=n
export JAVA_OPTS=-Xmx512m -XX:MaxPermSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,address=1025,server=y,suspend=n
Unified EL 2.2
The expression language is needed both in JSP when using tag libraries, and in facelets. The main improvements in the new version (see the
list of innovations ) - the ability to call methods, including with parameters.
${application.getRealPath('/data')}
Installation
- Remove lib / el-api.jar from Tomcat.
- Copy the implementation of el-api-2.2.jar and el-impl-2.2.jar into the lib folder in Tomcat. These are jars from Glassfish V3.
- Add the following lines to WEB-INF / web.xml in your application:
com.sun.faces.expressionFactory
com.sun.el.ExpressionFactoryImpl
Unfortunately, after updating the expression language, Jasper (implementation of the JSP compiler in Tomcat) does not understand the new expressions and they cannot be used in JSP. If you want Tomcat to use the new expression language everywhere (and not its implementation of org.apache.el), you need to recompile Tomcat. Details
here (in English). Also item 3 will not be needed.
Mojarra
The first implementation of JSF 2. Stable and fast. In addition to implementing JSF, we will also need to install JSTL 1.2.
Installation
- Download mojarra-2.0.2-FCS-binary.zip
- Copy from the archive from the mojarra-2.0.2-FCS / lib folder jsf-api.jar and jsf-impl.jar to the WEB-INF / lib folder of your application.
- Copy jstl 1.2 to the WEB-INF / lib folder of your application.
- Add the following lines to WEB-INF / web.xml in your application:
FacesServlet
javax.faces.webapp.FacesServlet
1
FacesServlet
*.xhtml
- Create the WEB-INF / faces-config.xml file in your application:
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-facesconfig_2_0.xsd"
version="2.0">
Weld
Weld implements the
JSR 299 Contexts and Dependency Injection specification. In brief, Weld allows you to set field values automatically using annotations.
Installation
- Download weld-1.0.1-Final.zip
- Create a WEB-INF / beans.xml file with the following contents:
It is necessary for Weld to scan the web application for Java-Beans that will participate in CDI. - Copy weld-1.0.1-Final / artifacts / weld / weld-tomcat-support.jar from the archive to the lib folder in Tomcat.
- Create a META-INF / context.xml file in your application with the following contents:
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
- Copy weld-1.0.1-Final / artifacts / weld / weld-servlet.jar from the archive to the WEB-INF / lib folder of your application.
- Add the following lines to WEB-INF / web.xml:
org.jboss.weld.environment.servlet.Listener
Object factory for the CDI Bean Manager
BeanManager
javax.enterprise.inject.spi.BeanManager
Hibernate validator
JSR 303 Bean Validation provides an easy way to apply validation rules for both JSF and JPA. These rules are defined through field
annotations in Java Beans:
@NotNull@Size(min = 1)
private String username;
Installation
- Download hibernate-validator-4.0.2.GA-dist.zip
- Copy hibernate-validator-4.0.2.GA/hibernate-validator-4.0.2.GA.jar from the archive to the WEB-INF / lib folder of your application.
- Copy the additional libraries slf4j-api-1.5.6.jar, validation-api-1.0.0.GA.jar from the folder in the hibernate-validator-4.0.2.GA/lib archive into the WEB-INF / lib folder of your application.
Eclipselink
JPA allows you to save Java Beans to your database without using SQL. The necessary calls and if necessary, the database schema is created automatically. It uses a simple
EntityManager interface . Unfortunately, because Tomcat is not a full-fledged Java EE server, some JPA features (transaction management, automatic receipt of EntityManager) are not available. Also, all objects that will be saved in the database should be described in the configuration file.
Installation
- This installation assumes that you have a MySQL database on the local computer. The password for the root of the database user is "" (empty password).
- Download the driver for the database and put the JAR in the lib folder in Tomcat.
- Download eclipselink-2.0.1.v20100213-r6600.zip
- Copy eclipselink / jlib / jpa / javax.persistence_2.0.0.v201002051058.jar from the archive to the WEB-INF / lib folder of your application.
- Copy eclipselink / jlib / eclipselink.jar from the archive to the WEB-INF / lib folder of your application.
- Create a WEB-INF / classes / META-INF / persistence.xml file in your application with the following contents:
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
org.eclipse.persistence.jpa.PersistenceProvider
your.class.Here -->
false
Download
Links
* Source code was highlighted with
Source Code Highlighter .