Servlet package
The Web application structure involving WEB-INF subdirectories is the standard for all Java web applications and is specified by the Servlet API specification. Given a top-level directory name myapp
the directory structure is as follows:
/myapp
/images
/WEB-INF
/classes
/lib
The WEB-INF subdirectory contains the deployment descriptor for the application, named web.xml
. All HTML files are located in the top-level directory myapp
. For admin users, you will find that the ROOT directory is the parent directory of myApp.
Create a Servlet in a package
The WEB-INF/classes directory contains all Servlet classes and other class files, and the directory structure of the class files matches their package name. For example, if you have a fully qualified class name com.myorg.MyServlet
, then the Servlet class must be located in the following directory
/myapp/WEB-INF/classes/com/myorg/MyServlet.class
The following example creates a package named com.myorg
the MyServlet class.
// Package Naming
package com.myorg;
// Import necessary Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Perform necessary initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html;charset=UTF-8");
// The actual logic is here
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// Nothing
}
}
Compile the Servlet in the package
Compiling classes in a package is not much different from compiling other classes. The easiest way is to keep your java file in a fully qualified path, such as the classes mentioned above, which will be kept in the com.myorg
. You also need to be in the CLASSPATH
add the directory.
Assuming that your environment is set up correctly, enter the <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes
directory, and compile MyServlet.java, as follows:
$ javac MyServlet.java
If Servlet depends on other libraries, you must also reference those JAR files in CLASSPATH. I only quote here. servlet-api.jar
JAR file, because I don’t use any other libraries in the Hello World program.
This command line uses the built-in javac compiler, which is included with the Sun Microsystems Java Software Development Kit (JDK, Java Software Development Kit). Microsystems’s Java Software Development Kit (JDK). In order for this command to work properly, you must include the location of the Java SDK that you use in the PATH environment variable.
If all goes well, the above compilation will be generated in the same directory MyServlet.class
files. The next section explains how to deploy a compiled Servlet to production.
Servlet packaged deployment
By default, Servlet applications are located in the path <Tomcat-installation-directory>/webapps/ROOT
and the class file is placed in the <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes
.
If you have a fully qualified class name com.myorg.MyServlet
then the Servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class
, you need to be located in the <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/
of web.xml
create the following entries in the file:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.myorg.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
The above entry is to be created in the web.xml
in the file <web-app>...</web-app>
inside the label. There may already be various entries available in this file, but don’t worry about it.
At this point, you are basically done, now let’s use <Tomcat-installation-directory>\bin\startup.bat
(on Windows) or <Tomcat-installation-directory>/bin/startup.sh
(on Linux/Solaris, etc.) start the tomcat server, and finally enter http://localhost:8080/MyServlet in the address bar of the browser. If all goes well, you will see the following results:
Hello World