The web application structure involving the WEB-INF subdirectory is the standard for all Java web applications and is specified by the Servlet API specification. Given a top-level directory named myapp, the directory structure is as follows:
/myapp
/images
/WEB-INF
/classes
/lib
The WEB-INF subdirectory contains the application's deployment descriptor, named web.xml. All HTML files are located under the top-level directory myapp. For the admin user, you will find that the ROOT directory is the parent directory of myApp.
The WEB-INF/classes directory contains all Servlet classes and other class files. The directory structure for the class files matches their package names. For example, if you have a fully qualified class name com.myorg.MyServlet, then this Servlet class must be located in the following directory:
/myapp/WEB-INF/classes/com/myorg/MyServlet.class
The following example creates a MyServlet class with the package name com.myorg.
// For package naming
package com.myorg;
// Import required 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 required 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");
// Actual logic is here
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy() {
// Do nothing
}
}
Compiling a class in a package is not much different from compiling other classes. The simplest way is to keep your Java file with the fully qualified path, as mentioned above for the class, which will be kept in com.myorg. You also need to add this directory to the CLASSPATH.
Assuming your environment is set up correctly, navigate to the <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes directory and compile MyServlet.java as follows:
$ javac MyServlet.java
If the Servlet depends on other libraries, you must also reference those JAR files in the CLASSPATH. Here, I only reference the servlet-api.jar JAR file because I am not using any other libraries in the Hello World program.
This command line uses the built-in javac compiler, which comes with the Sun Microsystems Java Software Development Kit (JDK). For this command to work properly, the location of the Java SDK you are using must be included in your PATH environment variable.
If everything goes well, the above compilation will generate the MyServlet.class file in the same directory. The next section will explain how to deploy a compiled Servlet to production.
By default, the Servlet application is located at the path <Tomcat-installation-directory>/webapps/ROOT, and the class files are placed in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.
If you have a fully qualified class name com.myorg.MyServlet, then this Servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class. You need to create the following entry in the web.xml file located at <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/:
<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 should be created within the <web-app>...</web-app> tags in the web.xml file. There may already be various entries available in this file, but don't worry about that.
At this point, you are basically done. Now, let's start the Tomcat server using <Tomcat-installation-directory>binstartup.bat (on Windows) or <Tomcat-installation-directory>/bin/startup.sh (on Linux/Solaris, etc.), and finally enter http://localhost:8080/MyServlet in the browser's address bar. If everything goes well, you will see the following result:
YouTip