<!-- Specifies what URI the above defined servlet is to be invoked -->
<!-- First line must be a servlet name defined above -->
<!-- second line specifies what URI the named servlet is invoked -->
<!-- URI: http://host/context-path/servlet/SimpleServlet-->
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/servlet/SimpleServlet</url-pattern>
</servlet-mapping>
<!-- URI: http://host/context-path/SimpleServlet
<servlet-mapping>
<servlet-name>AnotherServlet</servlet-name>
<url-pattern>/AnotherServlet</url-pattern>
</servlet-mapping>
-->
</web-app>
/WEB-INF/classes/
This directory contains all your servlet class files and all the other java classes files that are used in your application. Don't put any JAR files in this subdirectory. If your classes are in Java packages, you will have to define the package directory structure under this /WEB-INF/classes/ directory. For example, a Java class named edu.scranton.cs.bi.web/SimpleServlet.class would be stored in a file named /WEB-INF/classes/edu.scranton.cs.bi.web/SimpleServlet.class.
/WEB-INF/lib/
This directory is where you store all java JAR files, such as JDBC drivers.
Accessible classes/JARS in Tomcat
All classes in the WEB-INF/classes/ directory and classes in JAR files stored in the WEB-INF/lib/ directory, and classes stored in $TOMCAT_HOME/common/ and $TOMCAT_HOME/shared/ are made, by the Tomcat, visible and accessible to other classes within your web application. This works like as if Tomcat defined a CLASSPATH variable which include all those directories and all jar files when your application is executed under Tomcat. Thus, for example, if you want the PostgreSQL's JDBC driver, pg73jdbc3.jar, accessible in your application, simply copy it to your Context_Path/WEB-INF/lib/ directory.
Development and Deployment with Eclipse
When you create a Tomcat project using Eclipse, the directory structure described above is created automcatically by Eclipse. HTML files , e.g., SimpletServlet.htm , are stored in the root directory of the project, dot-java (.java) files, e.g., SimpleServlet.java, in the src directory, the JDBC drivers (JAR file), e.g., pg73jdbc3.jar, in WEB-INF/lib/. WEB-INF/classes/ is also created, but not displayed in Eclipse since the programmer does not need to access any files stored in this directory. WEB-INF/web.xml is created as a template, and you need to edit it to suit your application.
When the project is built and deployed (by selecting proper options in the Ant Build...), the dot-class (.class) files are generated and stored in WEB-INF/classes/, and then the directory structure is copied to $TOMCAT_HOME/webapps/. You may want to look into build.xml and build.properties two files to see how the configuration files for build and deployment.
Development and Deployment without any IDE
This part describes how to develop and deploy Web applications manually, i.e., with no help from any integrated development environment such as Eclipse. Hopefully, through this manual process, you have a better understanding of the directory structure of Tomcat and its relationship with the development environmnet.
Step 1: Create an directory structure for development
Create a directory named WebHello in C:\, and then create WEB-INF in WebHello\, and then WEB-INF\classes\ and WEB-INF\lib\ in WEB-INF. So we have:
C:\WebHello\
C:\WebHello\WEB-INF\
C:\WebHello\WEB-INF\classes\
C:\WebHello\WEB-INF\lib\
Step 2: Put files in proper places
Create SimpletServlet.htm and save it in C:\WebHello\ directory, and create SimpletServlet.java and save it in C:\WebHello\ directory
Step 3: Compile java servlets
Change directory to C:\WebHello\; and then compile SimpletServlet.java:
javac -d WEB-INF\classes -cp $TOMCAT_HOME\common\lib\servlet-api.jar SimpleServlet.java
Here, $TOMCAT_HOME refers to the path where your Tomcat is installed. If you have followed this tutorial from the beginning, you can compile your program using the following command (as one line):
javac -d WEB-INF\classes -cp "c:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar" SimpleServlet.java
Note that we are compiling the servlet program under Windows XP, and so we need to specify where the Java Servlet package is located in the command line using the -CLASSPATH (-cp) option. As mentioned in Accessible classes/JARS in Tomcat section, when this servlet is executed, the classes needed from servlet-api.jar would be found by Tomcat since it is stored in $TOMCAT_HOME/common/ directory.
Step 4: Copy the application to Tomcat for deployment
You may use the xcopy command to copy the WebHello directory and all files and subdirectories in it to $TOMCAT_HOME\webapps\ if you figure out the syntax of xcopy. However, the simplest way to do this step is, in Microsoft Windows XP's Explore, copy the WebHello directory and the paste it into $TOMCAT_HOME\webapps\. If you followed this tutorial, after copying you should see the following directory:
c:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\WebHello\
Step 5: Deploy the application by a Tomcat manager
Start your tomcat server and use a browser to log into the server as a manager, and deploy WebHello. Enter the following in a web browser
http://localhost:8081/WebHello/SimpleServlet.htm
Step 6: Test the application
In the displayed form, enter your first and last name as requested and then click submit. if everything goes well, it should display a message like "Hello, First Name Last Name, your first servlet works".
HttpServletRequest and HttpServletResponse API
HttpServletRequest API
The HttpServletRequest object passed by the WWW server to the doGet or doPost methods contains the request from the client. It provides a rich set of methods for accessing request parameters. A short list of those includes:
- String getParameter(String name): return the value of a parameter indicated by the name parameter as part of the GET or POST requests
- Enumeration getParameterNames(): return the names of all the parameters of a GET or POST request.
- String[] getParameterValues(String name): return an array of strings containing the values for a specified servlet parameter.
- Cookie[] getCookies(): return an array of Cookie objects stored on the client by the server. Cookies can be used to uniquely identify clients to the server.
- HttpSession getSession(boolean create): return an HttpSession object associated with the client's current browsing session. HttpSession severs a similar purpose as cookies.
HttpServletResponse API
The doGet and doPost methods receive an object that implements the HttpServletResponse interface. The interface provides a set of methods for sending information back to the client. A list of commonly used methods are listed below:
- void setContentType(String type): Specifies the type of response back to the browser. For example, "text/html" speecifies the response is an HTML document.
- PrintWriter getWriter(): returns a text-based output stream for text data to be sent back to the browser. For example, it can be used to print the HTML statements to the browser..
- ServletOutputStream getOutputStream(): return a binary output stream for binary data to be sent back to the browser.
- void addCookie(Cookie cookie): add cookies to the client's browser.