자바 웹 의 세 가지 생 성 Servlet 방식 설명

32026 단어 JavaJavaWeb
본 고 는 IDEA 를 이용 하여 Servlet 을 실현 하 는 세 가지 방법 을 소개 한다.
pom.xml 를 jar 패키지 에 의존 하도록 관리 합 니 다.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.kuanggroupId>
  <artifactId>servlet01artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>servlet01 Maven Webappname>
  
  <url>http://www.example.comurl>

  <dependencies>
      
      <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>servlet-apiartifactId>
        <version>2.3version>
      dependency>
   dependencies>
   <build>
    <finalName>servlet01finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

방법 1:Servlet 인터페이스 직접 실현

import javax.servlet.*;
import java.io.IOException;

//  servlet      :    Servlet  
public class servletBuildByServlet implements Servlet {

    //     (           )
    public servletBuildByServlet() {
        System.out.println("            ");
    }

    //     (   Servlet    )
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("   init     ");

    }

    //     (       ,     ,     )
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        System.out.println("service      ");

    }

    //  severlet
    //1.        
    //2.            
    public void destroy() {
        System.out.println("destroy        ");
    }

    //  servlet    
    public ServletConfig getServletConfig() {

        return null;
    }
    //  servlet   ,     
    public String getServletInfo() {

        return null;
    }

}

방법 2:GenericServlet 클래스 계승
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

//  GenericServlet 
public class servletBuildByGenericServlet extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("servlet Build By GenericServlet class");
    }
}

방법 3:HttpServlet 클래스 계승
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 servletBuildByHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("servlet Build By HttpServlet class.");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}


웹.xml 를 통 해 Servlet 를 등록 합 니 다.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
  
  <servlet>
    <servlet-name>servletBuildByServletservlet-name>
    <servlet-class>com.kuang.buildMethods.servletBuildByServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>servletBuildByServletservlet-name>
    <url-pattern>/sbbsurl-pattern>
  servlet-mapping>

  <servlet>
    <servlet-name>servletBuildByHttpServletservlet-name>
    <servlet-class>com.kuang.buildMethods.servletBuildByHttpServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>servletBuildByHttpServletservlet-name>
    <url-pattern>/sbbhsurl-pattern>
  servlet-mapping>

  <servlet>
    <servlet-name>servletBuildByGenericServletservlet-name>
    <servlet-class>com.kuang.buildMethods.servletBuildByGenericServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>servletBuildByGenericServletservlet-name>
    <url-pattern>/sbbgsurl-pattern>
  servlet-mapping>
web-app>

좋은 웹페이지 즐겨찾기