Você está na página 1de 5

[WEB TECHNOLOGIES] 4CSE1

APACHE TOMCAT SERVER


A number of servlet containers are available today. The most popular one—and the one recognized as the official servlet/JSP
container—is Tomcat. Originally designed by Sun Microsystems, Tomcat source code was handed over to the Apache
Software Foundation in October 1999. In this new home, Tomcat was included as part of the Jakarta Project, one of the
projects of the Apache Software Foundation. Working through the Apache process, Apache, Sun, and other companies—with
the help of volunteer programmers worldwide—turned Tomcat into a world-class servlet reference implementation.

The servlet container (Catalina) is based on a completely new architecture and has been developed from the ground up for
flexibility and performance. Another popular servlet container is JRun from Allaire Corporation. JRun is available in three
editions: Developer, Professional, and Enterprise. The Developer edition is free but not licensed for deployment.

Tomcat by itself is a web server. This means that you can use Tomcat to service HTTP requests for servlets, as well as static
files (HTML, image files, and so on). In practice, however, since it is faster for non-servlet, non-JSP requests, Tomcat normally
is used as a module with another more robust web server, such as Apache web server or Microsoft Internet Information
Server. Only requests for servlets or JSP pages are passed to Tomcat.

To write a servlet, you need at least version 1.2 of the Java Development Kit. The reference implementation for both servlets
and JSP are not included in J2SE, but they are included in Tomcat. Tomcat is written purely in Java.

Pre-requisite: jdk has to be installed on the system with all the paths set.

Installing TOMCAT
1. Download the zip file from http://tomcat.apache.org/download
2. Unzip the file and run the exe
3. Install the TOMCAT server to C://tomcat
4. Set CLASSPATH. Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the
servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) you
use for development probably doesn't. So, set the classpath.
a. Right-click on My Computer, goto the advanced tab, click on the environment variables.
b. Under user variables, create a new variable with the name as CLASSPATH and value as C:\Tomcat
5.5\common\lib\servlet-api.jar
5. Set JAVA_HOME. Set this environment variable to tell Tomcat where to find Java . Set this environment variable to
point at the top-level of Java installation directory.
a. Right-click on My Computer, goto the advanced tab, click on the environment variables.
b. Under user variables, create a new variable with the name as JAVA_HOME and value as C:\Program
Files\Java\jdk1.6.0_21
6. Change the port to 80. Making this change lets you use URLs of the form http://localhost/blah instead of
http://localhost:8080/blah
a. To change the port, edit install_dir/conf/server.xml and change the port attribute of the Connector element
from 8080 to 80, yielding a result similar to that below.
<Connector port="80" protocol="HTTP/1.1"
... >
7. Turn on servlet reloading. Tell Tomcat to check the modification dates of the class files of requested servlets, and
reload ones that have changed since they were loaded into the server's memory. The privileged entry is really to
support the invoker servlet, it can be omitted if the invoker servlet is not used.
a. To turn on servlet reloading, edit Edit install_dir/conf/context.xml and change
<Context>
To
<Context reloadable="true" privileged="true">
8. Enable the invoker servlet. The invoker servlet lets you run servlets without first making changes to your Web
application's deployment descriptor (i.e., the WEB-INF/web.xml file). Instead, you just save your servlet (Eclipse) or

Y.Lakshmi, Assistant Professor, CSE Department, VNR VJIET 1


[WEB TECHNOLOGIES] 4CSE1

drop your servlet into WEB-INF/classes (manual deployment) and use the URL http://host/servlet/ServletName (or
http://host/webAppName/servlet/packageName.ServletName once you start using your own Web applications and
packages).
a. uncomment the following servlet and servlet-mapping elements in install_dir/conf/web.xml
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
...
</servlet>
...
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

To test whether Tomcat was installed properly, open your browser on the same computer you used to
install Tomcat and direct it to the following URL:
http://localhost:8080
For a successful tomcat installation, the browser will open page similar to this:

Tomcat Directories
When you install Tomcat, the installation program creates a number of directories under %CATALINA_HOME% [Tomcat
installation directory]. Understanding the function of each subdirectory is important to configuring Tomcat and deploying
your servlet and JSP applications.
Directory Description
bin Startup and shutdown scripts and other files.
classes Unpacked classes global to web applications.
conf Configuration files including server.xml (Tomcat's main configuration file) and the global web.xml
(deployment descriptor) file.
server Tomcat's archive files.
lib Common classes in .jar files.
logs Tomcat's log files.
common Common classes for both Catalina and web applications.
webapps Servlet and JSP applications.
work Resulting servlets from the JSP pages translation

Y.Lakshmi, Assistant Professor, CSE Department, VNR VJIET 2


[WEB TECHNOLOGIES] 4CSE1

The standard directory layout:


The entire directory structure can be archived in a Web application archive file. Such an archive is known as a WAR file and
ends with the .war file extension. IfaWAR file is placed in the webapps directory, then, when the Tomcat server begins
execution, it extracts the contents of the WAR file into the appropriate webapps subdirectory structure. To facilitate creation
of a Web Application Archive file in the required format, it is convenient to arrange the "executable" files of your web
application (that is, the files that Tomcat actually uses when executing your app) in the same organization as required by the
WAR format itself. To do this, you will end up with the following contents in your application's "document root" directory:
/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 and
container-managed security constraints that you want the server to enforce for you. This file is discussed in more
detail in the following subsection.
/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.

Execute a servlet on TOMCAT:

1. Create a directory called myApp under the webapps directory. The directory name is important because this also
appears in the URL to your servlet.
2. Create the and WEB-INF directories under myApp, and create a directory named classes under WEB-INF. The
directory classes under WEB-INF is for your Java classes. If you have HTML files, put them directly under the myApp
directory. You may also want to create a directory called images under myApp for all your image files.
3. Write the servlet source code
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class TestingServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}
4. Save your TestingServlet.java file to the WEB-INF/classes directory under myApp. Placing your source code here will
make it inaccessible from a web browser. Static files, such as HTML files and image files, should be placed directly
under the myApp directory or a directory under it. Placing your source code files outside the WEB-INF directory will
make them viewable from a browser.

Y.Lakshmi, Assistant Professor, CSE Department, VNR VJIET 3


[WEB TECHNOLOGIES] 4CSE1

5. Compile the source code


javac TestingServlet.java

6. Create the Deployment Descriptor


A deployment descriptor is an optional component in a servlet application. The descriptor takes the form of an XML
document called web.xml and must be located in the WEB-INF directory of the servlet application. When present, the
deployment descriptor contains configuration settings specific to that application.

To create the deployment descriptor, you now need to create a web.xml file and place it under the WEB-INF directory
under myApp. The web.xml for this example application must have the following content.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>

The web.xml file has one element—web-app. You should write all your servlets under <web-app>. For each servlet,
you have a <servlet> element and you need the <servlet-name> and <servlet-class> elements. The <servlet-name> is
the name for your servlet, by which it is known Tomcat. The <servlet-class> is the compiled file of your servlet
without the .class extension.

Having more than one servlet in an application is very common. For every servlet, you need a <servlet> element in
the web.xml file. For example, the following shows you how web.xml looks if you add another servlet called Login:

<?xml version="1.0" encoding="ISO-8859-1"?>


<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
</web-app>

7. Run tomcat

8. Call Your Servlet from a Web Browser.


Now, you can call your servlet from a web browser. By default, Tomcat runs on port 8080 in the myApp virtual
directory under the servlet subdirectory. The servlet in this example is named Testing. The URL for that servlet has
the following format:
http://domain-name/virtual-directory/servlet/servlet-name
Any static file can be accessed using the following URL:
http://domain-name/virtual-directory/staticFile.html

Y.Lakshmi, Assistant Professor, CSE Department, VNR VJIET 4


[WEB TECHNOLOGIES] 4CSE1

If you run the web browser from the same computer as Tomcat, you can replace the domain-name part with
"localhost". In that case, the URL for your servlet is
http://localhost:8080/myApp/servlet/Testing

In the deployment descriptor you wrote in Step 4, you actually mapped the servlet class file called TestingServlet with
the name "Testing," so that your servlet can be called by specifying its class file (TestingServlet) or its name (Testing).
Without a deployment descriptor, you must call the servlet by specifying its class name; that is, TestingServlet. This
means that if you did not write a deployment descriptor in Step 4, you need to use the following URL to call your
servlet:
http://localhost:8080/myApp/servlet/TestingServlet

Y.Lakshmi, Assistant Professor, CSE Department, VNR VJIET 5

Você também pode gostar