美文网首页
自定义servlet

自定义servlet

作者: dancer4code | 来源:发表于2020-09-24 23:17 被阅读0次

两种方式

  • @WebServlet注解+@ServletComponentScan
  • 通过配置FilterRegistrationBean的方式

1. @WebServlet注解+@ServletComponentScan

servlet

package com.d4c.custombean.servlet;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@WebServlet(name="FirstServlet",urlPatterns = "/servlet/*",loadOnStartup = 1)
public class FirstServlet implements Servlet {

    private ServletConfig servletConfig;
    private ServletRequest servletRequest;
    private ServletResponse servletResponse;
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig = config;
        log.info("FirstServlet init-------");
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        log.info("FirstServlet service-------");
    }

    @Override
    public String getServletInfo() {
        return servletRequest.getServletContext().getServerInfo();
    }

    @Override
    public void destroy() {
        log.info("FirstServlet destroy-------");
    }
}

启动类

package com.d4c.custombean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan
@SpringBootApplication
public class CustomBeanApplication {
    public static void main(String[] args) {
        SpringApplication.run(CustomBeanApplication.class, args);
    }
}

@WebServlet常用属性

属性 类型 是否必须 说明
asyncSupported boolean 指定Servlet是否支持异步操作模式
displayName String 指定Servlet显示名称
initParams WebInitParam[] 配置初始化参数
loadOnStartup int 标记容器是否在应用启动时就加载这个Servlet
name String 指定Servlet名称
urlPatterns/value String[] 这两个属性作用相同,指定Servlet处理的url

也可以继承HttpServlet

package com.d4c.custombean.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebServlet(name="SecondServlet",urlPatterns = "/servlet2/*",loadOnStartup = 2)
public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("SecondServlet doGet-------");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("SecondServlet doPost-------");
    }


    @Override
    public void init() throws ServletException {
        log.info("SecondServlet init-------");
    }

    @Override
    public void destroy() {
        log.info("SecondServlet destroy-------");
    }
}

2.通过配置FilterRegistrationBean的方式

package com.d4c.custombean.servlet;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@WebServlet(name="ThirdServlet",urlPatterns = "/servlet3/*",loadOnStartup = 3)
public class ThirdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("ThirdServlet doGet-------");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("ThirdServlet doPost-------");
    }


    @Override
    public void init() throws ServletException {
        log.info("ThirdServlet init-------");
    }

    @Override
    public void destroy() {
        log.info("ThirdServlet destroy-------");
    }
}

package com.d4c.custombean.config;

import com.d4c.custombean.filter.SecondFilter;
import com.d4c.custombean.servlet.ThirdServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRegisterBean {

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean();
        //注册自定义过滤器
        bean.setServlet(new ThirdServlet());
        //过滤器名称
        bean.setName("ThirdServlet");
        //过滤所有路径
        bean.addUrlMappings("/servlet3");
        //优先级
        bean.setOrder(3);
        return bean;
    }

}

推荐用第二种

相关文章

网友评论

      本文标题:自定义servlet

      本文链接:https://www.haomeiwen.com/subject/elioyktx.html