Spring Boot-Servlet使用

Web开发使用Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。当使用Spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。

Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

 

方法一:通过ServletRegistrationBean进行代码注册

Servlet代码:

public class Servlet1 extends HttpServlet{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        System.out.println("Servlet初始化");
        super.init();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----------> doGet()");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----------> doPost()");
        resp.setContentType("text/html");  
        String id = req.getParameter("id");
        if(id != null && !id.equals("")){
            resp.getWriter().append("-----------> doPost()  id=" + id);
            return;
        }
        resp.getWriter().append("-----------> doPost()");
    }
}

注册代码:

@Bean
public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new Servlet1(),"/servlet/*");// ServletName默认值为首字母小写,即servlet
}

 

 

方法二:使用注解注册Servlet

Servlet代码:

/**
 * 使用注解注册Servlet
 * 
 */

//不指定name的情况下,name默认值为类全路径,即org.springboot.sample.servlet.MyServlet2
@WebServlet(urlPatterns="/servlet/servlet2", description="Servlet的说明") 
public class Servlet2 extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        System.out.println("Servlet初始化");
        super.init();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----------> doGet()");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----------> doPost()");
        resp.setContentType("text/html");  
        String id = req.getParameter("id");
        if(id != null && !id.equals("")){
            resp.getWriter().append("-----------> doPost()  id=" + id);
            return;
        }
        resp.getWriter().append("-----------> doPost()");
    }
}

 

添加@ServletComponentScan注解:

使用注解方式注册Servlet需要在SpringbootHelloApplication里添加@ServletComponentScan注解

@SpringBootApplication
@ServletComponentScan //使用注解的方式注册servlet需要在SpringbootHelloApplication.java中添加@ServletComponentScan注解
public class SpringbootHelloApplication {   
    public static void main(String[] args) {
        SpringApplication.run(SpringbootHelloApplication.class, args);
    }
}

注意:可以通过@ServletComponentScan注解里的basePackages属性指定加载指定包下的Servlet。

 

修改DispatcherServlet默认配置

    /**
     * 修改DispatcherServlet默认配置
     *
     * @param dispatcherServlet
     */
    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.getUrlMappings().clear();
        registration.addUrlMappings("*.action"); //只有*.action 的请求能通过
        registration.addUrlMappings("*.json");
        return registration;
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值