Latest web development tutorials

Servlet examples

Servlet service HTTP requests and implementjavax.servlet.Servlet interface Java classes.Web application developers often write Servlet extend javax.servlet.http.HttpServlet, abstract class and implements the Servlet interface is designed to handle HTTP requests.

Hello World Sample Code

Here is the Hello World Servlet output sample source code:

// Import necessary java library import *;
import javax.servlet *.;
import javax.servlet.http *.;

// Extend 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");

      // Actual logic is here PrintWriter out = response.getWriter ();
      out.println ( "<h1>" + message + "</ h1>");
  public void destroy ()
      // do nothing}

Compile Servlet

Let's put the above code written in file, put the file C: \ ServletDevel (on Windows) or / usr / ServletDevel (on UNIX), you also need to add these directories to the CLASSPATH .

Assuming that your environment is correctly set, enterServletDevel directory, and compile, as follows:

$ Javac

If Servlet depend on any other libraries, you must include those JAR file in the CLASSPATH. Here, I only included servlet-api.jar JAR file, because I do not use any other library in the Hello World program.

The command line uses Sun Microsystems Java Software Development Kit (JDK) built javac compiler. For this command to work properly, you must position the Java SDK PATH environment variable to use.

If all goes well, the compiler will generate above HelloWorld.class file in the same directory. The next section will explain how to deploy the compiled Servlet in production.

Servlet Deployment

By default, Servlet application in the path under <Tomcat-installation-directory> / webapps / ROOT, and the class files in the <Tomcat-installation-directory> / webapps / ROOT / WEB-INF / classes in.

If you have a fully qualified class namecom.myorg.MyServlet, then the Servlet class must be in WEB-INF / classes / com / myorg / MyServlet.class in.

Now, let's copy HelloWorld.class to <Tomcat-installation-directory> / webapps / ROOT / WEB-INF / classes in, and located in <Tomcat-installation-directory> / webapps / ROOT / WEB-INF / theweb Create the following entry .xmlfile:



The above entries are to be created in the web.xml file <web-app> inside ... </ web-app> tag. In this file you may have a variety of items available, but do not care.

Here, you are almost done, now let's use the <Tomcat-installation-directory> \ bin \ startup.bat (on Windows) or <Tomcat-installation-directory> /bin/ (in Linux / Solaris and so on) to start tomcat server, and finally enter thehttp in the browser's address bar: // localhost: 8080 / HelloWorld.If all goes well, you will see the following results:

Servlet examples