Home  >  Article  >  Java  >  How are Java Servlets deployed and run?

How are Java Servlets deployed and run?

WBOY
WBOYOriginal
2024-04-17 09:33:01865browse

Java Servlets are deployed and run through Servlet containers. Deployment involves writing a Servlet class, packaging it as a WAR file, and copying it to the container's deployment directory. The container loads the WAR file, creates a Servlet instance, and calls Servlet methods to generate a response when the client requests it. For example, to deploy a servlet using Tomcat, first define the servlet and its URL mapping, and then package it together with the Servlet class into my-servlet.war. Copy the WAR file to Tomcat's webapps directory and start the server. Accessing the specified URL runs the servlet, which generates a response containing "Hello World!"

Java Servlet是如何部署和运行的?

Deploying and running Java Servlet

Overview

Java Servlet is a Java Web component used to generate dynamic Web content. It is deployed and run via a Servlet container such as Tomcat or Jetty.

Deploy Servlet

Servlet deployment involves the following steps:

  1. Develop Servlet: Write a Servlet class and inherit javax.servlet. Servlet interface.
  2. Package Servlet: Package the Servlet class into a WAR (Web Archive) file. A WAR file is a ZIP file that contains the Servlet class, other dependent libraries, and a deployment descriptor (web.xml).
  3. Copy the WAR file: Copy the WAR file to the deployment directory of the Servlet container.

Running Servlet

The Servlet container is responsible for running the deployed Servlet. When the client requests the URL corresponding to the Servlet:

  1. The container loads the WAR file: The Servlet container loads the WAR file that contains the Servlet class and descriptor.
  2. Create Servlet instances: The container creates an instance for each Servlet.
  3. Call the Servlet's life cycle methods: The container calls the Servlet's init(), service() and destroy() Method to initialize, handle requests and destroy Servlet.
  4. Generate response: The Servlet generates a response in HTML, JSON, or other formats.

Practical case

The following is an example of using Tomcat to deploy and run a Servlet:

web.xml (deployment descriptor):

<web-app>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
  </servlet-mapping>
</web-app>

MyServlet.java (Servlet class):

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("<h1>Hello World!</h1>");
  }
}

Deploy and run:

  1. willweb.xml and MyServlet.java are packaged into a WAR file named my-servlet.war.
  2. Copy my-servlet.war to Tomcat’s webapps directory.
  3. Start Tomcat.
  4. Visit http://localhost:8080/myServlet in the browser. You should see a page that says "Hello World!"

The above is the detailed content of How are Java Servlets deployed and run?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn