Skip to main content

JavaEE and Tomcat Basic Tutorial in Ubuntu 12.04

If you are interested in developing JavaEE with Linux and don’t want to use any IDE, or you are just looking around how JavaEE and Tomcat works with Ubuntu 12.04, well here is a fast and simple tutorial for you. Our object is to create a weight index calculator!

JavaEE and Tomcat Environment #

First we will create the JavaEE development environment by installing JDK and Tomcat.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install openjdk-7-jdk
sudo apt-get install tomcat7

Next we will edit Tomcat configurations.

sudoedit /etc/tomcat7/server.xml

Add the following lines inside and edit your own USERNAME to the lines.

<Context docBase="/home/USERNAME/jsp" path="/jsp">

JavaEE File Structure in Tomcat #

Create the jsp/ folder into your home folder.

mkdir jsp

Move inside the folder and create the basic file structure.

cd jsp/
mkdir WEB-INF
mkdir WEB-INF/classes
mkdir META-INF
touch WEB-INF/web.xml

Minimal web.xml #

Move to WEB-INF folder and edit configuration file for our JavaEE web application.

nano web.xml

Here is the minimal web.xml file you can have, except I have included one servlet in here which we are going to use later. Paste this into the opened file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""

JavaEE classes in Tomcat #

We are going to create one class to help our application, these classes are also called beans.

cd classes
mkdir index
cd index

We will create simple Java class.

package index;
public class index {
	float height;
	float weight;
	float index;
	public void setWeight(float weight) {
	public float getWeight() {
		return weight;
	public void setHeight(float height) {
	public float getHeight() {
		return height;
	public float getIndex() {
		return index;

Now we need to compile this file with javac.


Important note here! This is not a good style to compile the classes, you should have the .java files somewhere else and then just import the .class files here, because it is not secure to have original files here on the web interface. But since this is only a tutorial and we are learning here, this will do.

JSP files #

Now we will create the .jsp files which will be shown to user as they browse our web application.

cd jsp/
nano weight.jsp

Add this code into the file.

<%@ page info="Weight index with JSP" %>
<jsp:useBean id="indexbean" scope="page" class="index.index"/>
<jsp:setProperty name="indexbean" property="*"/>
<title>Weight Index with JSP</title>
<h2>Please give your weight in kg and height in meters.</h2>
<form action="calculator.jsp" method="GET">
		<td>Your weight</td>
		<td><input type="text" name="weight"/></td>
		<td>Your height</td>
		<td><input type="text" name="height"/></td>
<input type="submit" value="Calculate"/>

And add another file

nano calculator.jsp
<%@ page info="Showing the result" %>
<jsp:useBean id="indexbean" scope="page" class="index.index" />
<jsp:setProperty name="indexbean" property="*" />
<title>Weight Index with JSP</title>
<h2>Your weight index is <jsp:getProperty name="indexbean" property="index" /></h2>

Starting Tomcat and testing the application #

Now we have created all of our files and want to start our server and see the webpages. Type in the following command and restart the Tomcat server.

sudo service tomcat7 restart

Open up web browser and go to http://localhost:8080/jsp/weight.jsp and you will see our application.

Tomcat Form

Give it your info and continue to the next page which will make use of our java class.

Tomcat Result

There are alot more in the world of JavaEE but this is was just a simple crash course into the subject using Ubuntu 12.04.