Master of Computer Science - University of Grenoble I - France
Contact: Sara Bouchenak (Sara.Bouchenak@imag.fr)
Building (distributed) web applications
Introduction to Servlet programming
Java 2 SDK
IDE (e.g. Eclipse)
Organize your directory as described in the figure below.
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
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
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
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:
Copy the ~gautiesa/M1Info/IBD/Soft/apache-tomcat-6.0.14.tar.gz
file in your working directory (e.g. in ~/IBD/Intergiciel/TP3/).
This file is also available for download here
apache-tomcat-6.0.14.tar.gz.
Unzip the apache-tomcat-6.0.14.tar.gz file, as follows: tar xfz apache-tomcat-6.0.14.tar.gz.
Define the CATALINA_HOME environment variable in such a way that it points to your installation directory of Apache Tomcat, as follows:
In Unix / tcsh: setenv CATALINA_HOME ~/IBD/Intergiciel/TP3/apache-tomcat-6.0.14
In Unix / bash: export CATALINA_HOME=~/IBD/Intergiciel/TP3/apache-tomcat-6.0.14
In Windows: set CATALINA_HOME=~/IBD/Intergiciel/TP3/apache-tomcat-6.0.14
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:
Make sure that the port number specified in the Servlet server configuration file is not used.
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:
Open a web browser and type the following URL http://<host>:<port>/ where <host> and <port> are respectively the host name and the port on which the Servlet server is running.
Follow the different links to test the Servlet-based web application that provides several Servlet examples.
Stopping the Servlet server
In order to stop the Apache Tomcat Servlet server, please do the following:
Run $CATALINA_HOME/bin/shutdown.sh on a Unix machine (or $CATALINA_HOME/bin/shutdown.bat for Windows).
4.1. Hello
Run the two Servlet examples from a web browser. What do they do?
Examine the directory content of $CATALINA_HOME/webapps/.
$CATALINA_HOME/webapps/ contains one sub-directory per web application (e.g. $CATALINA_HOME/webapps/ROOT/ is the directory of the default web application, i.e. the application accessible via http://<host>:<port>/).
A web application deployed on the Servlet server is organized as follows (see http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html for more details):
*.html, etc. - The HTML pages, along with other files that must be visible to the client browser (such as images or text files) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy (e.g. $CATALINA_HOME/webapps/ROOT/ contains the index.html file and the src/ sub-directory that contains Java source files).
/WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters.
/WEB-INF/classes/ - This directory contains
any Java class files (and associated resources) required for your application,
including both servlet and non-servlet classes, that are not combined into JAR
files. If your classes are organized into Java packages, you must reflect this
in the directory hierarchy under /WEB-INF/classes/
. For example, a
Java class named com.mycompany.mypackage.MyServlet
would need to be
stored in a file named /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class
.
WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.
Examine, in particular, the content of the following files or directories: $CATALINA_HOME/webapps/ROOT/index.html, $CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml, $CATALINA_HOME/webapps/ROOT/classes/
4.2. Being John Malkovich
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?
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
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?
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
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?
Extend this Servlet in order to add the following information to the generated response:
the request method
the request parameters (names and values)
the request header (names and vaules)
the request cookies (names and values).
4.5. Quid
Copy the QuidServlet servlet, compile it and integrate it to the web application.
What are the parameters passed to this servlet? Run the QuidServlet with the appropriate parameters.
What does this Servlet do?
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:
Copy the SessionServlet servlet, compile it and integrate it to the web application.
Run the SessionServlet; what are the returned results?
Reload the SessionServlet several times in the web browser; what are the produced results?
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?
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:
Copy the Counter servlet, compile it and integrate it to the web application.
Run the Counter servlet. What are the returned results?
Reload the Counter servlet several times in the web browser; what are the produced results?
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?
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?
Use the appropriate tools provided by the web browser to delete cookies, and run again the Counter servlet. What are the produced results?
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?
[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/