How to use J2EE

Page outline

More information

Basics

Just like you have your very own copy of Postgres that you can manage you have an instance of the Java Application server J2EE. Like mentioned above, all the J2EE files reside in the following directory:

/opt/j2eeXX/

You will note that there is a huge amount of documentation of the J2EE server available, which might be a little overwhelming at first. While you won't have to read it all, you can't get out of reading at least certain parts of it. One of the most important documents is Chapter 1: Getting Started of the administration guide.

While reading the documentation you must keep in mind that your installation uses different port numbers than the default ones. Here is a quick overview:

Description Default port number New port number
Admin console 4848 48XX
HTTP web server 8080 80XX

As always XX is the number of your user account.

The values above are those for the default administrative domain called domain1. Every J2EE server can run multiple so-called domains, each of which occupies different port numbers. For our project, however, there is no need to set up additional domains, you can use domain1 for all your work. If you still want to set up a second domain for testing, you must make sure that the port numbers do not conflict with those reserved for other groups. (Everything above 10000 should be save to use.)

The Administration Console

The administration console is the control center of your J2EE instance. It allows you to carry out all kinds of tasks like changing your password, deploying applications, etc.

To access it simply point your web browser at:

http://lsir-cis-pcY:48XX

This gives you the login screen where you enter the J2EE login data you have received via e-mail. After logging in you get to the welcome page ofthe administration console which looks like this:

Screenshot of the Administration Console

Screenshot of the Administration Console (click to enlarge)

Changing your J2EE administrator password is a good opportunity to get acquainted with the interface. The necessary instructions can be found here.

Note that you don't need to understand most of the settings. Some of the more interesting ones can be found right on the Common tasks page. Another interesting section is the list of Web Applications, but you will get to know this in more detail later on during the project.

Basic concepts

In J2EE there are a few very basic concepts that you should understand:

Tools

Java application server technologies rely on a number of tools that you can find in your /opt/j2eeXX/bin directory. These are the ones you will use most often:

Another very important tool of a developer of web applications is of course the logfile where the J2EE server keeps track of errors (e.g. exceptions in Java code). By default it can be found at the following location:

/opt/j2eeXX/domains/domain1/logs/server.log

Even though the J2EE server usually outputs error messages to the client, you may find more complete information in that logfile. [More information on debugging J2EE applications]

JavaServer Pages (JSP)

JavaServer Pages are a powerful technology suitable especially to create dynamic web content. There are a few technologies based on JSP, some of which you will get to know in more detail as part of the project.

For starters you should have a look at some JSP components in action. We have prepared a few examples that should be easy to understand yet powerful enough so you can build your own applications based on the demonstrated techniques:

JSP examples

After you have seen how it works you should download the sources:

[JSP_Time.tar.gz] [JSP_SQLTag.tar.gz]

You can unpack the archives by entering tar xfz archive.tar.gz.

The Time JSP example

Let's start with the first example by examining the files and directories contained in the archive:

Now let's build the example and deploy it to your server. Before you get started make sure that the admin-password.txt contains the correct password. If that's the case, run the build-war target to compile and package the module:

user01@lsir-cis-pc1:~/JSP_Time$ asant build-war
Buildfile: build.xml

prepare:
    [mkdir] Created dir: /home/user01/JSP_Time/build

copy:
     [copy] Copying 7 files to /home/user01/JSP_Time/build

build:
    [javac] Compiling 1 source file to /home/user01/JSP_Time/build

build-war:
     [echo] Creating the WAR ...
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF/classes
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF
     [copy] Copying 6 files to /home/user01/JSP_Time/assemble/war
      [war] Building war: /home/user01/JSP_Time/assemble/war/time.war
     [copy] Copying 1 file to /home/user01/JSP_Time

BUILD SUCCESSFUL
Total time: 2 seconds
user01@lsir-cis-pc1:~/JSP_Time$

If everything went fine you should now have a file (the web archive) named time.war whose contents you can inspect using the unzip tool if you're interested:

user01@lsir-cis-pc1:~/JSP_Time$ l time.war
-rw-rw----  1 user01 user01 30053 Mar 27 01:34 time.war
user01@lsir-cis-pc1:~/JSP_Time$ unzip -l time.war
Archive:  time.war
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  03-27-05 01:34   META-INF/
      107  03-27-05 01:34   META-INF/MANIFEST.MF
      837  03-27-05 01:34   index.html
    24388  03-27-05 01:34   garfield.gif
      744  03-27-05 01:34   timelet.jsp
      549  03-27-05 01:34   static.html
     1832  03-27-05 01:34   dynamic.jsp
      751  03-27-05 01:34   WEB-INF/timefuncs.tld
      314  03-27-05 01:34   WEB-INF/sun-web.xml
      883  03-27-05 01:34   WEB-INF/classes/iis/Time.class
     1282  03-27-05 01:34   time.jsp
      385  03-26-05 21:29   WEB-INF/web.xml
 --------                   -------
    32072                   12 files
user01@lsir-cis-pc1:~/JSP_Time$

Now you can already deploy the application to your J2EE server using the deploy target like so:

user01@lsir-cis-pc1:~/JSP_Time$ asant deploy
Buildfile: build.xml

prepare:

copy:

build:

build-war:
     [echo] Creating the WAR ...
   [delete] Deleting: /home/user01/JSP_Time/assemble/war/time.war
   [delete] Deleting directory /home/user01/JSP_Time/assemble/war/WEB-INF
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF/classes
     [copy] Copying 1 file to /home/user01/JSP_Time/assemble/war/WEB-INF
      [war] Building war: /home/user01/JSP_Time/assemble/war/time.war
     [copy] Copying 1 file to /home/user01/JSP_Time

deploy:

admin_command_common:
     [echo] Doing admin task deploy time.war
[sun-appserv-admin] Executing: deploy --port 4801 --host localhost
--passwordfile "admin-password.txt"  --user admin time.war
[sun-appserv-admin] Command deploy executed successfully.

BUILD SUCCESSFUL
Total time: 4 seconds
user01@lsir-cis-pc1:~/JSP_Time$

You should now be able to access the web application at the following URL:

http://lsir-cis-pcY.epfl.ch:80XX/time

Tip: Sometimes you may want to access your web server from the console because you only have SSH access or just because it's faster. There is a text browser called ELinks installed that you can launch by simply entering elinks. Press q to quit, F9 to enter the menu, and \ to switch to HTML source mode (very useful for debugging JSP pages!). Everything else you need to know can be found in the man page and the online help.

The SQLTag example

The next example we're going to look at demonstrates the SQL Tag library which is part of the JSP Standard Tag Library (JSTL). This library provides a quick and dirty way of accessing a database from within a JSP page.

If you look at the source code you will see that the entire functionality is contained in a single file, namely index.jsp. It combines some of the techniques that you have seen in the Time example above. Let's look at a few key points:

Building and deploying this example works in much the same way as for the Time example above. Try it! (Make sure the empty directory src exists when you try to build the application. Alternatively you can also adapt the build.xml.) After you have deployed the application it is left up to you to find the right URL to access it in your web browser. :-)

Using the Deployment Tool

Next to using the asant build system there is another way to package and deploy web components. The Deployment Tool that comes with J2EE has a graphical interface and doesn't need any configuration files set up beforehand. Once again though, it is probably a better idea to use the build.xml files from the above examples and adapt them to your needs. You will see why in a minute. :-)

For the demonstration we will deploy another copy of the SQLTag example. So before we start you should create a copy of the JSP_SQLTag directory (say JSP_SQLTag2) and delete all files and directories therein except for web/index.jsp.

To start the Deployment Tool you must once again activate X forwarding or have the tool installed locally. If that is the case you can start it by entering deploytool on the command line. The following window should appear after a moment:

Screenshot of the J2EE Deployment tool after the first start

The J2EE Deployment tool after the first start (click to enlarge)

Step 1: Adding the server

First of all you have to add your J2EE server to the servers list. Choose File|Add Server... from the menu and enter the machine name and port number of your J2EE server. By checking the Target Server check box you make this server the default for deploying applications.

Select the new server in the list on the left and enter the username and password when you're asked. This should bring up the list of currently deployed applications.

Step 2: Creating a new web component

Choose File|New|Web Component... from the menu, which will launch the New Web Application Wizard. Click Next to skip the introduction screen.

Step 3: Choosing a location for the WAR file

Click Browse... in the WAR Naming area and select a place to store the .war file, e.g. JSP_SQLTag2/sqltag2.war. Don't worry about the WAR Display Name.

Step 4: Specify the Context Root

The Context Root field corresponds to the context-root XML element in the sun-web.xml file we have used above for asant. Pick a path where the component will be reachable, e.g. /sqltag2.

Step 5: Adding the content

Now comes the most important part, which is the selection of the content we want to add to the web archive. Click the Edit Contents... button to bring up the content chooser. Select the web/index.jsp file and then click the Add button:

Screenshot of the content chooser

Adding content to the web component (click to enlarge)

Click the OK button to return to the wizard window:

Screenshot of the WAR file settings window

The WAR file settings window (click to enlarge)

Click Next to continue.

Step 6: Select the component type

Since we are about to package a JavaServer Page, pick the JSP Page option and click Next to go to the final step of the wizard.

Step 7: Define component properties

In the final step you can set some general properties of the component you are about to package. The only important setting here is the JSP Filename, where you should choose the main page, i.e. index.jsp in our case. You can leave the Web Component Name and Web Component Display Name as they are. (They will later appear in the subcomponent list in the admin console but are not visible otherwise.)

Screenshot of the general properties window

The general properties window (click to enlarge)

Now click Finish to close the wizard window.

Step 8: Checking your settings

Now you should see deploytool's main window again with all the settings that you have entered in the wizard:

Screenshot of the deploytool with the created WAR file open

The deploytool with the created WAR file open (click to enlarge)

Step 9: Deploying the web component

If everything is set up correctly you can deploy the application. Either choose 'Tools|Deploy...' from the menu or click the Deploy icon in the toolbar. After entering the username and password a status window should appear displaying the progress of the deployment process:

Screenshot of the status window after successful deployment

The status window after successful deployment (click to enlarge)

Done!

If everything went fine you can close the status window and access the web application in your browser.

Copyright © Martin Rubli & Patrik Bless – Last change:
This page uses valid XHTML 1.0 Strict and valid Cascading Style Sheets, Level 2. This page uses valid XHTML 1.0 Strict. This page uses valid Cascading Style Sheets, Level 2.