Server Installation

Introduction:

The development and execution environment for this course is based on the CSE standard Linux environment found in the labs. The following two elements are absolutely critical for this course
  1. Java 1.6
  2. Tomcat 7.0.42

Tomcat 7 is not provided in the CSE environment by default and needs to be set up in the manner that will be described shortly. To set up a development environment on your own machine, please follow these instructions. Please note that no other versions of Java or Tomcat are supported. We will use the CSE lab machine as the official environment for demonstrations and evaluations. Therefore, make sure that your own development environment is compatible with the course environment.

The recommended IDE for this course is Eclipse IDE v 4.2 (Juno). In theory, you can use any IDE but officially only Eclipse will be supported.

This instruction assumes that you are at one of the CSE labs, using a standard CSE machine.

Apache Tomcat Application Server

Simply put, Apache Tomcat (or just Tomcat) (http://tomcat.apache.org/) is an open-source project provides a Web server plus Servlet Container environment together so that we can program Web applications using Java.

The version we are going to use is 7.0.42 (which supports Servlet/JSP specification version 3.0/2.2).

Installation

In your home directory (e.g. /home/srikumarv), run:

% tar -zxvf /home/cs9321/download/apache-tomcat-7.0.42.tar.gz -C .

The above command will create a directory called apache-tomcat-7.0.42 and install Tomcat under it.

Setting Up To Start Tomcat

There are three environment variables you need to set before you can start Tomcat: JAVA_HOME, CATALINA_HOME and CATALINA_BASE. They should be set as follows:

CATALINA_BASE=/home/YOUR_LOGIN_NAME/apache-tomcat-7.0.42
CATALINA_HOME=/home/YOUR_LOGIN_NAME/apache-tomcat-7.0.42
JAVA_HOME=/usr/lib/jvm/java-6-sun

Do:

% cd /home/YOUR_LOGIN_NAME/apache-tomcat-7.0.42
% cp /home/cs9321/public_html/download/setTomcat .

Then open the file with a text editor. Set the value of each variable as shown above. Then, to set the variables in the environment, run the command:

% source /home/YOUR_LOGIN_NAME/apache-tomcat-7.0.42/setTomcat

If you are starting Tomcat manually yourself, you should set these environment variables every time you are working with Tomcat. You also need to run set these variables for every new xterm you open.

Double check that the variables are set correctly: For example, you can do:

% echo $CATALINA_HOME
/home/YOUR_LOGIN_NAME/apache-tomcat-7.0.42
% echo $JAVA_HOME
/usr/lib/jvm/java-6-sun

Now change directory to the CATALINA_HOME/bin directory (e.g., /home/srikumarv/apache-tomcat-7.0.42/bin) and run the Tomcat startup script.

% cd $CATALINA_HOME/bin
% sh startup.sh

You should see something like:

Using CATALINA_BASE:   ... apache-tomcat-7.0.42
Using CATALINA_HOME:   ... apache-tomcat-7.0.42
Using CATALINA_TMPDIR: ... apache-tomcat-7.0.42/temp
Using JRE_HOME:       ... java-6-sun

You can verify that the server is running by checking the URL "http://localhost:8080" from the browser. You should see something like in Figure 1:

http://localhost:8080
Default Server Configuration
Figure 1  Tomcat running ...

If you do not see the page, first make sure you don't have anything else already on your machine using that port. For example, running the nmap command tells me whether the port is closed (not used) or open (used):

% nmap localhost -p 8080     

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2007-08-04 22:20 EST
Interesting ports on localhost (127.0.0.1):
PORT     STATE  SERVICE
8080/tcp closed http-proxy

% nmap localhost -p 7000-9000

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2007-08-04 22:23 EST
All 2001 scanned ports on localhost (127.0.0.1) are closed

The default port setting is 8080. Most of the time, this should be fine. If something else is running on the port and you cannot use this port, you can change the setting by modifying the file located at:

$CATALINA_HOME/conf/server.xml

Search for '8080' in the file. You will see that the HTTP1.1 Connector (the Coyote!) is set to run at port 8080. You can change the port setting to something else (anynumber between 1024 and around 65000). Save the file and stop, then start the server again.

Since Tomcat 6.0, a user with manager role is required to start the Tomcat Manager. This role is not put in by default. You have to create the user manager and assign it to athe role manager-gui (Read more about Tomcat 7 roles here: docs) by editing the file $CATALINA_HOME/conf/tomcat-users.xml. For the time being, add this line at the end of the list of users:

<user username="manager" password="tomcat" roles="manager-gui"/>
Please replace the password with a more secure alternative.

Now, you can access the manager from the Tomcat start page. If you click on the Manager App button on the start page, you will see all the Web applications currently deployed under the host (localhost).

In Tomcat Manager, you are able to stop/restart/deploy/undeploy Web applications to Tomcat.

From http://localhost:8080 page, also try a few examples that came with Tomcat. Try to execute them and browse the source code.

Log files Tomcat server creates are located under: $CATALINA_HOME/logs. Open some of the non-empty log files and check out the content.

Tomcat installation also comes with documentation. You can access it through the main page (http://localhost:8080) and Tomcat Documentation Link.

To stop the server, open a new Xterm window and type in the following:

% source setTomcat
% cd $CATALINA_HOME/bin
% sh shutdown.sh

The installation can be simply deleted and re-installed from the zip file anytime.

These are some of the key tomcat directories, all relative to $CATALINA_HOME:

Getting Started with Eclipse and WTP

Now that Tomcat is ready, let us move on to the Eclipse tool and Web Tools Platform (WTP) plugins. Before you do, be sure to stop the Tomcat server, otherwise when WTP tries to start Tomcat, you will get an error message telling you that the ports may already be in use.

The WTP plugin is already installed at CSE labs under /usr/bin/eclipse. Start eclipse by running:

% eclipse

We will do the following in Eclipse:

  1. Add a Server Runtime Environment (i.e., Tomcat you just installed)
  2. Create a Web application project
  3. Create and Edit a simple JSP
  4. Run the JSP on the Tomcat Server

Adding a Server Runtime: Choose Window - Preferences - Server - Installed Runtimes. Choose 'Add' and 'Apache Tomcat v7.0'. Browse to your Tomcat installation directory (i.e., your own $CATALINA_HOME) and click Finish.

Create a Dynamic Web Project: Eclipse workspace contains a set of projects. You typically put projects that are related to each other in the same workspace. Each project has a set of builders associated with it. Builders are what give the project its intelligence. Builders know how to process the artifacts in a project. WTP provides builders for Java Web applications. These builders know, for example, how to package the artifacts in J2EE modules so that they can be deployed to J2EE application servers.

  1. Create a new project via File - New - Project and then open New Project wizard.
  2. Select Web - Dynamic Web project.
  3. Click the Next button.
  4. Give 'myproject' as the name of the project, keep clicking the Next button until you reach the very end of the wizard screen (leaving everything as it is, by default).
  5. Select and tick the checkbox 'Generate web.xml deployment descriptor'.

You should get a new project structure named 'myproject' in your Project Explorer

Create and edit a JSP file: Add a new JSP file to the WebContent folder of Web1 as follows. Right click on myproject's WebContent folder, and invoke 'New JSP'. Give the new file the name hello_world.jsp. The wizard lets you pick a template for the new JSP. Select the template for JSP with HTML markup, and click the Finish button.

The wizard creates the JSP file from the template and opens it in the JSP editor. The JSP editor provides full content assist on HTML tags, JSP tags, and Java code scriptlets. Edit the JSP to say “Hello, world” using HTML tags. The Web application is now ready to run.

Run the JSP on the server: Eclipse WTP extends the 'Run As' command to Web artifacts such as HTML and JSP files. Simply select the file and invoke the Run As, then Run on Server command from the context menu.

To run the JSP file, select hello_world.jsp and right click to invoke the 'Run As' - 'Run on Server' from the context menu. Since this is the first time you have tried to run any artifact from myproject, WTP will prompt you to associate a new server for the project. WTP defaults the server runtime environment to Apache Tomcat, which you created before.

Click the Next button to advance to the Add and Remove Projects page, and Click the Finish button to confirm that you want WTP to add the myproject module to the server configuration. WTP then starts the server and opens a Web browser with the Uniform Resource Locator (URL) of the JSP file (see Figure 6). You should also see the same if you copy-paste the URL and open it through your own browser (e.g., Firefox, Explorer)