English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Servlet Instance

Servlet is a service for HTTP requests and implementation javax.servlet.Servlet The Java class of the interface. Web application developers usually write Servlet to extend javax.servlet.http.HttpServlet and implement the abstract class of the Servlet interface specially used to handle HTTP requests.

Hello World example code

The following is an example of the source code for Servlet to output Hello World:

// Import the necessary java library
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extends HttpServlet class
public class HelloWorld extends HttpServlet {
 
  private String message;
  public void init() throws ServletException
  {
      // perform 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);
      // The actual logic is here
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1);
  }
  
  public void destroy()
  {
      // does nothing
  }
}

Compile Servlet

Let's write the above code in the HelloWorld.java file, place this file in C:\ServletDevel (on Windows) or /usr/ServletDevel (on UNIX) and you also need to add these directories to the CLASSPATH.

Assuming your environment is correctly set up, enter ServletDevel directory, and compile HelloWorld.java as follows:

$ javac HelloWorld.java

If the Servlet depends on any other libraries, you must include those JAR files in the CLASSPATH. Here, I only included the servlet-api.jar JAR file, as I did not use any other libraries in the Hello World program.

This command line uses the javac compiler built into the Sun Microsystems Java Software Development Kit (JDK). To make this command work properly, the PATH environment variable needs to be set to the path of the Java SDK.

If everything goes well, the compiled HelloWorld.class file will be generated in the same directory. The next section will explain how to deploy the compiled Servlet in production.

Servlet Deployment

By default, the Servlet application is located at the path <Tomcat-installation-directory>/webapps/ROOT directory, and the class file is placed under <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/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/MyServlet.class.

Now, let's copy HelloWorld.class to HelloWorld.class inside <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes directory, and located within <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/ file's web.xml Create the following entry in the

<web-app>      
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

The above entries need to be created under the <web-app>.../web-app> tag inside. There may already be various available entries in this file, but don't worry about them.

Up to this point, you have basically completed. 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 the browser: http://localhost:8080/HelloWorldIf everything goes well, you will see the following result: