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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s