Servlet Packages
The Web application structure involving the WEB-INF subdirectory is 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 in the top-level directory myapp Under it. For the admin user, you will find that the ROOT directory is the parent directory of myApp.
Creating a Servlet in a Package
The WEB-INF/classes directory contains all Servlet classes and other class files, with the directory structure of the class files matching 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 .
// Name the package
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("" + message + "
");
}
public void destroy()
{
// do nothing
}
}
Compiling a Servlet in a Package
Compiling a class in a package is not much different from compiling other classes. The simplest way is to have your java file retain the fully qualified path; for the class mentioned above, it will be kept in com.myorg. You also need to add that directory to the CLASSPATH.
Assuming your environment is set up correctly, enter the
$ javac MyServlet.java
If the Servlet depends on other libraries, then you must also reference those JAR files in the CLASSPATH. Here I only reference the servlet-api.jar JAR file because I did not use 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, fully Java Software Development Kit). Microsystems' Java Software Development Kit (JDK). For this command to work, the location of the Java SDK you use must be included in the PATH environment variable.
If all goes well, the above compilation will generate MyServlet.class in the same directory. The next section explains how to deploy a compiled Servlet to production.
Servlet Packaging and Deployment
By default, a Servlet application is located in the path
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 in
MyServlet
com.myorg.MyServlet
MyServlet
/MyServlet
The above entry should be created within the
At this point, you are basically done. Now let's use
Hello World |
|---|