English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Involved in WEB-The INF subdirectory's Web application structure 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
WEB-The INF subdirectory contains the application deployment descriptor, named web.xml. All HTML files are located in the top-level directory myapp Below. For the admin user, you will find that the ROOT directory is the parent directory of myApp.
WEB-INF/The classes directory contains all Servlet classes and other class files, and the directory structure of 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 package named com.myorg The MyServlet class.
// Name the package package com.myorg; // Import the 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 { // Execute the necessary initialization message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set the 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() { // do nothing } }
There is no big difference between compiling classes in a package and compiling other classes. The simplest method is to keep your java file with the fully qualified path, as mentioned above, the class will be retained in com.myorg. You also need to add this directory to the CLASSPATH.
Assuming your environment is correctly set up, go into <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, then you must also reference those JAR files in the CLASSPATH. Here I only referenced 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 is included with the Sun Microsystems Java Software Development Kit (JDK, the full name is Java Software Development Kit). Sun Microsystems' Java Software Development Kit (JDK). To make this command work properly, you must include the location of the Java SDK you are using in the PATH environment variable.
If everything goes well, the above compilation will generate the same directory MyServlet.class file. 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/Below the ROOT directory, and the class file is placed in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/in the classes directory.
If you have a fully qualified class name com.myorg.MyServletThen, this Servlet class must be located in the WEB-INF/classes/com/myorg/In the MyServlet.class file, you need to be located in the directory <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/ Create the following entry in the web.xml 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 entries need to be created in the <web-app>.../web-app> tag within. There may already be various entries available in this file, but don't worry about them.
By now, you have basically completed the task. Now let's use the <Tomcat-installation-directory>\bin\startup.bat (on Windows) or <Tomcat-installation-directory>/bin/startup.sh (on Linux)/Start the tomcat server on systems like Solaris, and finally enter the following in the address bar of your browser: http://localhost:8080/MyServletIf everything goes well, you will see the following result:
Hello World |