JavaWeb之三种创建Servlet方式讲解

本文介绍使用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>

方法一:直接实现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;
    }

}

方法二:继承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");
    }
}

方法三:继承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);
    }
}

通过web.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>

你可能感兴趣的:(Java,JavaWeb)