Servlet instance


Release date:2023-12-12 Update date:2023-12-12 Editor:admin View counts:84

Label:

Servlet instance

Servlet is the service HTTP request and implementation javax.servlet.Servlet the Java class of the interface. Web application developers usually write Servlet to extend the javax.servlet.http.HttpServlet and an abstract class that implements theServlet interface is specifically used to handle HTTP requests.

Hello World sample code

The following is a sample source code for the Servlet output 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("<h1>" + message + "</h1>");
  }

  public void destroy()
  {
      // Nothing
  }
}

Compile Servlet

Let’s write the above code in HelloWorld.java in the file, put the fileon C:ServletDevel (on Windows) or /usr/ServletDevel on UNIX, you also need to add these directories to the CLASSPATH .

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

$ javac HelloWorld.java

If Servlet depends on any other libraries, you must use the CLASSPATH Contains those JAR files. Here, I only include servlet-api.jar the JAR file, because I didn’t 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 the command work properly PATH the environment variable needs to set the path to the Java SDK.

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

Servlet deployment

By default, Servlet applications are located in the path < Tomcat-installation-directory > / webapps/ROOT, and class files are placed in < Tomcat-installation-directory > / webapps/ROOT/WEB-INF/classes.

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

Now, let’s put HelloWorld.class copy to < Tomcat-installation-directory > / webapps/ROOT/WEB-INF/classes, and in the < Tomcat-installation-directory > / webapps/ROOT/WEB-INF/ web.xml create the following entries in the file:

<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 entry is to be created in the web.xml in the file <web-app>...</web-app> inside the label. There may already be various entries available in this file, but don’t worry about it.

At this point, you are basically done, now let’s use <Tomcat-installation-directory>\bin\startup.bat (on Windows) or <Tomcat-installation-directory>/bin/startup.sh (on Linux/Solaris, etc.) start the tomcat server, and finally enter http://localhost:8080/HelloWorld in the address bar of the browser. If all goes well, you will see the following results:

Image0

Powered by TorCMS (https://github.com/bukun/TorCMS).