Master of Computer Science - University of Grenoble I - France


Introduction to Distributed Systems

Contact: Sara Bouchenak (Sara.Bouchenak@imag.fr)


Programming with Java Servlets

  1. Objectives

  2. Software environment

  3. Servlet environment

  4. Questions

  5. References

1. Objectives

2. Software environment

Used software

Directory organization

Organize your directory as described in the figure below.

How to compile a Java program

Type the following command line:

TP3> javac -d classes src/*.java

If your program uses a Java library (e.g. lib1.jar), type the following command line:

TP3> javac -classpath .:classes:lib/lib1.jar -d classes src/*.java

How to run a Java program

If your program's main Java class is C1, type the following command line:

TP3> java -classpath .:classes C1

If your program uses a Java library (e.g. lib1.jar), type the following command line:

TP3> java -classpath .:classes:lib/lib1.jar C1

How to generate a Java doc

Type the following command line:

TP3> javadoc -d doc -sourcepath src

More details on Java tools can be found in Sun Microsystems' web site: http://java.sun.com/j2se/1.5.0/docs/tooldocs

3. Servlet environment

The Servlet server used here is Apache Tomcat (http://tomcat.apache.org/).

Installing the Servlet server

In order to install Apache Tomcat Servlet server, please follow these steps:

  1. Download Apache Tomcat software here apache-tomcat-6.0.14.tar.gz

  2. Unzip the apache-tomcat-6.0.14.tar.gz file, as follows: tar xfz apache-tomcat-6.0.14.tar.gz

  3. Define the CATALINA_HOME environment variable in such a way that it points to your installation directory of Apache Tomcat, as follows:

  4. Make sure that the JAVA_HOME environment variable is defined (check it with: echo $JAVA_HOME); otherwise, define it in such a way that it points to the installation directory of Java.

Starting the Servlet server

The Apache Tomcat Servlet server is started on a particular port specified in the $CATALINA_HOME/conf/server.xml configuration file. The default port for accepting HTTP connections is 8080 (see the corresponding "Connector port" value in the server.xml configuration file).

In order to start the Apache Tomcat Servlet server, please follow these steps:

  1. Make sure that the port number specified in the Servlet server configuration file is not used.

  2. Run $CATALINA_HOME/bin/startup.sh on a Unix machine (or $CATALINA_HOME/bin/startup.bat for Windows).

Accessing a Servlet-based web application

In order to access a web application hosted by the Servlet server, please do the following:

Stopping the Servlet server

In order to stop the Apache Tomcat Servlet server, please do the following:

4. Questions

4.1. Hello

  1. Run the two Servlet examples from a web browser. What do they do?

  2. Examine the directory content of $CATALINA_HOME/webapps/.

4.2. Being John Malkovich

  1. Type the following URL request on your web browser http://<host>:<port>/servlet/HelloToServlet?firstname=John&lastname=Malkovich
    (where <host> and <port> are respectively the host name and the port on which the Servlet server is running).
    What does this request do?

  2. Modify the application in order for a web client to be able to perform a "Hello To John Malkovich" request directly from the http://<host>:<port> main page.
    What are the web application's parts/files that are impacted by this change?

4.3. Come back

  1. Modify the application in order for a web client to be able, from the web page resulting from a "Hello World" request, to return back to the http://<host>:<port> main page (i.e. the index.html page).
    What are the web application's parts/files that are impacted by this change?

  2. Modify the application in order for a web client to be able, from the web page resulting from a "Hello To" request, to return back to the http://<host>:<port> main page (i.e. the index.html page).
    What are the web application's parts/files that are impacted by this change?

4.4. Happy birthday

  1. Write a new Servlet which, tbased on a date of birth (year, month and day, e.g. 1985/09/27), calculates the day-of-week corresponding to that date (e.g. Friday).
    Integrate that Servlet to the web application and run it.
    What are the web application's parts/files that are impacted by this extension?

  2. Extend this Servlet in order to add the following information to the generated response:

4.5. Quid

  1. Copy the QuidServlet servlet, compile it and integrate it to the web application.

  2. What are the parameters passed to this servlet? Run the QuidServlet with the appropriate parameters.

  3. What does this Servlet do?

  4. Start the Servlet server (Apache Tomcat), run the QuidServlet with some parameters, and shutdown ths Servlet server. Then start the Servlet server once again and run the QuidServlet with some parameters. What do you notice? How do you explain this behavior?

4.6. Who am I? Session and cookie management

Sessions:

  1. Copy the SessionServlet servlet, compile it and integrate it to the web application.

  2. Run the SessionServlet; what are the returned results?

  3. Reload the SessionServlet several times in the web browser; what are the produced results?

  4. Shut down the Servlet server (while keeping the web browser running), and reload the SessionServlet several times in the same web browser. What are the produced results?

  5. Shut down the web browser (while keeping the Servlet server running), restart the browser and run again the SessionServlet. What do you notice? How do you explain it?

Cookies:

  1. Copy the Counter servlet, compile it and integrate it to the web application.

  2. Run the Counter servlet. What are the returned results?

  3. Reload the Counter servlet several times in the web browser; what are the produced results?

  4. Shut down the Servlet server (while keeping the web browser running), and reload the Counter servlet several times in the same web browser. What are the produced results?

  5. Shut down the web browser (while keeping the Servlet server running), restart the browser and run again the Counter servlet. What do you notice? How do you explain it?

  6. Use the appropriate tools provided by the web browser to delete cookies, and run again the Counter servlet. What are the produced results?

  7. Have a look at the API of the setMaxAge method of the javax.servlet.http.Cookie class. How is this method used in the Counter servlet program?

5. References

[Sun07a] Sun Microsystems. Java Servlet Technology. http://java.sun.com/products/servlet/

[Sun07b] Sun Microsystems. JavaTM 2 Platform Standard Edition 5.0 - API Specification. http://java.sun.com/j2se/1.5.0/docs/api/

[Tomcat07]    Apache Tomcat. http://tomcat.apache.org/