SpringBoot servlet

1.写一个servlet  类

package sprintbootstudy.springbootquickstart.servlet;

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 {
        super.doGet(req, resp);
    }

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

再写一个配置类  @Configuration  注册三大组件

Servlet  Filter Listener

ServletRegistrationBean:

package sprintbootstudy.springbootquickstart.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sprintbootstudy.springbootquickstart.servlet.Myservlet;

@Configuration
public class myserverConfig {
    //注册三大组件
    @Bean  //加在容器中
    public ServletRegistrationBean myservlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new Myservlet(),"/myservlet");
        return registrationBean;
    }

}

Filter:拦截器

  @Bean   //filter是拦截器
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myservlet"));
        return registrationBean;
    }






package sprintbootstudy.springbootquickstart.controller;


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

public class MyFilter implements Filter {   //注意 这个是javax.servlet下的包
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter process....");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

Listener:     监听web销毁或者是生成的

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener());
        return servletListenerRegistrationBean;
    }



package sprintbootstudy.springbootquickstart.controller;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {  //重写启动和销毁的方法
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized....web 启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed...web  销毁");
    }
}

Servlet 容器可以切换  不只是有Tomcat

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值