TutorialTomcat5 5 9
TutorialTomcat5 5 9
0 Introduction
Tomcat, developed by Apache (www.apache.org), is a standard
reference implementation for Java servlets and JSP. It can
be used standalone as a Web server or be plugged into a Web
server like Apache, Netscape Enterprise Server, or Microsoft
Internet Information Server. There are many versions of
Tomcat. This tutorial uses Tomcat 5.5.9 as an example. The
tutorial should also apply to all later versions of Tomcat.
Figure 1
The default Tomcat page is displayed.
4 Creating a Servlet
A servlet resembles an applet in some extent. Every Java
applet is a subclass of the Applet class. You need to
override appropriate methods in the Applet class to
implement the applet. Every servlet is a subclass of the
HttpServlet class. You need to override appropriate methods
in the HttpServlet class to implement the servlet. Listed
below is a simple servlet example that generates a response
in HTML using the doGet method.
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
/** Handle the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
// output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello, Java Servlets");
out.println("</body>");
out.println("</html>");
out.close();
}
}
5 Compiling Servlets
Suppose you have installed Tomcat 5.5.9 at c:\jakartatomcat-5.5.9. To compile FirstServlet.java, you need to add
c:\jakarta-tomcat-5.5.9\common\lib\servlet-api.jar to the
classpath from DOS prompt as follows:
set classpath=%classpath%;c:\jakarta-tomcat-5.5.9\common\lib\servlet-api.jar
10
The map entry maps an internal servlet name with a URL using
the following syntax:
<servlet-mapping>
<servlet-name>Internal Name</servlet-name>
<url-pattern>URL</url-pattern>
</servlet-mapping>
NOTE
<Side Remark: download web.xml>
For your convenience, I have created the web.xml
that contains the decriptions for running all
servlets in this chapter. You can download it
from
www.cs.armstrong.edu/liang/intro6e/supplement/we
b.xml.
TIP
<side remark: Web development tools>
11
7 Running Servlets
To run the servlet, start a Web browser and type
http://localhost:8080/liangweb/FirstServlet in the URL, as
shown in Figure 2.
Figure 2
You can request a servlet from a Web browser.
NOTE: You can use the servlet from anywhere on
the Internet if your Tomcat is running on a host
machine on the Internet. Suppose the host name
is liang.armstrong.edu; use the URL http://
liang.armstrong.edu:8080/liangweb/FirstServlet
to test the servlet.
NOTE: If you have modified the servlet, you need
to shut down and restart Tomcat.
8 Running JSP
To run a JSP (e.g., Factorial.jsp), store it in
webapps/liangweb, start a Web browser and type
http://localhost:8080/liangweb/Factorial.jsp in the URL, as
shown in Figure 3.
12
Figure 3
You can request a JSP from a Web browser.
13
14
...
<security-constraint>
<web-resource-collection>
<web-resource-name>
Protected Site
</web-resource-name>
<!-- This would protect the entire site -->
<url-pattern> /* </url-pattern>
<!-- If you list http methods,
only those methods are protected -->
<http-method> DELETE </http-method>
<http-method> GET </http-method>
<http-method> POST </http-method>
<http-method> PUT </http-method>
</web-resource-collection>
<auth-constraint>
<!-- Roles that have access -->
<role-name> test </role-name>
</auth-constraint>
</security-constraint>
<!-- BASIC authentication -->
<login-config>
<auth-method> BASIC </auth-method>
<realm-name> Example Basic Authentication </realm-name>
</login-config>
<!-- Define security roles -->
<security-role>
<description> Test role </description>
<role-name> test </role-name>
</security-role>
..
4. Start Tomcat.
5. Type http://localhost:8080/liangweb/FirstServlet from a
Web browser, you will be prompted to enter user name and
password, as shown in Figure 4.
15
Figure 4
Tomcat supports basic authentication using the Memory realm.
16
17
4. Start Tomcat.
5. Type http://localhost:8080/liangweb/FirstServlet from a
Web browser, you will be prompted to enter user name and
password, as shown in Figure 4.
Figure 5
Tomcat supports basic authentication using the Database
realm.
18