Configuring JSF to work on Tomcat

This project is available on GitHub https://github.com/stuartbrand/JSFforTomcat

I have been using Glassfish and Weblogic for many years and they have been very good to work with. They have lots of features and a comprehensive administration interface.

Glassfish – I noticed that the more Web applications I deployed, the slower the server became. I have one server that even after removing all of the applications and just leaving one it still took 2-3 minutes just to load the list of installed applications. Even clean installs are not as responsive and lean as I would like them to be with each instance taking 500mb of ram just to start up. No good for Microservices.

Weblogic – As with Glassfish, Weblogic has an administrative interface with lots of configurable features. And Weblogic, just like Glassfish,  can also be heavy on the resources, needing 500-1000mb ram just to start up!

So I started to look into other JavaEE application servers. There are lots of JavaEE servers, some of which are derivatives of the open source Glassfish, such as Payara, which is a commercially supported version of Glassfish with patches and updates.

I didn’t want to use just another version of Glassfish or Weblogic, then I came across Tomcat.

Tomcat is small, the install is only 15mb! and when running it only uses 140mb of ram to start up.

Tomcat is also fast, really fast, from the log file on a cold start “Server startup in 360 ms” compare this to the 1-2 minutes that Glassfish or Weblogic can take. And application deployment, again, fast! where Glassfish and Weblogic can take minutes, Tomcat will do the same deployment in seconds.

So, it’s fast, small, quick to deploy, what are the down sides?

Tomcat is lightweight… but this also means it doesn’t do a lot by itself. Tomcat runs on the JVM and doesn’t come with any extra’s.

What do I mean? Glassfish has built in support for Jax-RS and JSF, Weblogic has a suit of extra’s like ASF out of the box. All you do is tell your code that you want to use ASF components and deploy.

So when I tried to move applications over to Tomcat I found out very quickly the limitations of the out of the box server. However, with a little configuration of your web app, Tomcat can handle everything any JavaEE server can.

Now the background is out of the way, let’s get JSF working.

Java Server Faces, or JSF, is a Java specification for building component-based user interfaces for web applications. From your Classes/Beans you can dynamically work with methods from a web page. Working with input forms and buttons to control your methods allows you to create feature rich web apps.

Like I said before, Tomcat does not have a lot features out of the box and you have to configure your web app to use the JSF specification.

This is simple if you are using a Maven based project, a little harder if not, but not too hard. If you don’t use Maven then you will need to download the .jar files and add them to your library.

To start, create a new Maven project. Right click on the new project and go to Properties. Navigate to Frameworks and Add JavaServer Faces. This will configure the web.xml to use the JSF servlet filter but it also adds the following dependancy to your projects pom.xml that isn’t compatible with Tomcat.

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.7</version>
</dependency>

Delete this dependancy and add the following dependancies so Maven downloads the required libraries for Tomcat.

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.13</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.13</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.10.Final</version>
</dependency>

You may have noticed the weld library, this is a helper servlet that will allow JSF to access your Classes.

Add the following to your web.xml file.

<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

One more important configuration that I sometimes miss and it drives me crazy when troubleshooting is the requirement of a special file that serves no purposes other then enabling JSF to use your Classes. It’s called beans.xml.

To add beans.xml just right click on your project and New -> Other… and choose the category “Contexts and dependancies” you’ll find the Beans.xml in there, just add it to your project.

Create a new Class called SaySomething with the following contents

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class SaySomething {
public String getHello() {
return “Hello”;
}
}

There are 2 important parts to this class, @Named and @RequestScoped. @Named will make this a managed bean and usable by JSF, @RequestScoped defines the lifecycle of the bean, in this case it will only last in memory for the time of the request.

It is very important to use javax.enterprise.context.RequestScoped and not javax.faces.bean.RequestScoped as the latter will not work with Tomcat.

Now you can use the bean from a JSF page. Open the index.xhtml file that was created when you added the JavaServer Faces framework.

Put the following into the body area #{saySomething.hello}

That’s it, run the application and you should see a web page pop up with the word Hello.

Now you have a JSF configured web app you can add more classes and methods and use these inside JSF components like

<h:outputText value=”#{saySomething.hello}” />

I will be writing other blogs on how to use these components but for now this is a configured running application that is compatible with Tomcat.

I hope this has helped someone and I look forward to your comments.

Connecting to a database with Tomcat

Browsing the internet for instructions on how to connect to a database from my Java web app using Tomcat was a minefield.

I almost gave up on Tomcat as so many sites would give overly complex methods involving changing the configuration files of the Tomcat server itself. How could that be? I don’t have access to the configuration files of hosted services like Amazon, so there must be another way… and there is.

I discovered there are easy steps to allow your web app to connect to a database.

I am using Netbeans, so the following information is for Netbeans, however, you may be able to adapt it to whatever IDE you use. I will be discussing only how to create the connection in this post, but not how to use them, that will be in other posts.

Before you can use the database connection in your app you must first register the database connection with the IDE. Under the services tab, right click on the database element and select “New connection”. Follow the instruction to connect to your database. On the last step, where you are asked to set the JNDI name, do not go with the default. Instead try to follow the preferred naming convention of jdbc/myDatabase as this will help when connecting to it later.

You will need to use the correct driver for your database. Java has a lovely translation system that allows you to write SQL queries using a standard syntax called JPA. The drive will then convert this SQL into the vendor specific syntax to work with the database you are connecting to.

You can either add the .jar file from the vendors download page to your projects library, or use maven by adding the correct tags to your projects pom.xml file if you are using a maven project(a good idea).

Here is the MySql one;

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
</dependency>

And this one works with Microsoft Sql;

<dependency>
<groupId>com.hynnet</groupId>
<artifactId>sqljdbc4-chs</artifactId>
<version>4.0.2206.100</version>
</dependency>

Now you have the correct driver and the IDE can connect to your database you can configure your web app to use the database.

Right click on your project, select New and then “Entity classes from database”. When you select the JNDI name you created earlier Netbeans will retrieve all of the Tables and Views (that have primary keys) from your database. Select the ones you want and next, next, finish. This will create the Entity classes for you.

As I said earlier, I wont go into detail in this post on how to use the Entity classes. Search my other posts for help with that.

So, you have your Entity’s. You have connected your IDE to your database. Can I just publish my app to Tomcat? Unfortunately, No.

Unlike other Java application servers, such as Glassfish, Payara or Weblogic, the war file does not create JDBC connections in the server.

However, it is rather easy to do in Tomcat once you know how.

To tell Tomcat how you want your web app to use the database you need to edit the WEB-INF/web.xml file and add the following tags.

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Now you can use your database from your beans. I use the following code to do this.

public EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(“jdbc/myDatabase”);
return emf.createEntityManager();
}

public List<EntityTableName> getEntityTableName() {
EntityManager em = getEntityManager();
List<EntityTableName> results = (EntityTableName) em.createNamedQuery(“EntityTableName.findAll”).getResults();
em.close();
return results;
}

That’s it! I can now use the getEntityTableName() method to pull the results from the database and use them as needed.

I hope this helped someone and I look forward to your comments.