Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Servlet Example

Servlet Example

A Servlet is a Java class that serves HTTP requests and implements javax.servlet.Servlet interface. Web application developers usually write Servlets to extend javax.servlet.http.HttpServlet and implement the abstract class of the Servlet interface specifically designed to handle HTTP requests.

Hello World Sample Code

The following is sample source code for a Servlet that outputs Hello World:



// Import necessary Java libraries

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;



// Extend HttpServlet class

public class HelloWorld 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");



      // The actual logic is here

      PrintWriter out = response.getWriter();

      out.println("

" + message + "

"); } public void destroy() { // do nothing } }

Compiling the Servlet

Let's write the code above 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 set up correctly, enter the 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 have only included the servlet-api.jar JAR file because 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). For this command to work, the PATH environment variable needs to be set to the Java SDK path.

If all goes well, the above compilation will generate a HelloWorld.class file in the same directory. The next section explains how a compiled Servlet is deployed in production.

Servlet Deployment

By default, a Servlet application is located in the path /webapps/ROOT, and the class files are placed in /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.

Now, let's copy HelloWorld.class to /webapps/ROOT/WEB-INF/classes, and in the file located at /webapps/ROOT/WEB-INF/ web.xml create the following entry:



      

    

        HelloWorld

        HelloWorld

    



    

        HelloWorld

        /HelloWorld

    

  

The above entry should be created within the ... tag in the web.xml file. There may already be various entries in that file, but don't worry about them.

At this point, you are basically done. Now let's use \bin\startup.bat (on Windows) or /bin/startup.sh (on Linux/Solaris, etc.) to start the tomcat server, and finally, in the browser's address bar, enter http://localhost:8080/HelloWorldIf all goes well, you will see the following result:

Servlet Example