精华内容
下载资源
问答
  • servletregistrationbean
    2022-04-03 20:08:18

    关于使用ServletRegistrationBean导致servlet失效问题

    关于使用ServletRegistrationBean导致servlet失效问题

    使用SpringBoot自定义servlet

    发现在使用自定义servlet的时候,访问页面失效
    项目中使用了spring-boot-starter-web依赖,spring-boot会在底层自动导入一个DispatchServlet
    DispatcherServlet的默认匹配规则为/**
    自定义的servlet如果加了urlPatters,则优先使用自定义的servlet,会使配置的拦截器也无法拦截,因为没有走dispatcherServlet的doDispatch方法

    还有可能是因为使用servletRegistrationBean没有配置好专属的urlPatter所以失效

    更多相关内容
  • ServletRegistrationBean与ServletListenerRegistrationBean

    servlet

    在app里面加入@ServletComponentScan

    一种为

    @WebServlet(value = {"/hello"})
    @Slf4j
    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                     response.setContentType("text/html;charser=uts-8");
            PrintWriter writer = response.getWriter();
            writer.print("nihao");
            log.info("hhhhhh");
        }
    }

    使用ServletRegistrationBean可以不用写

    @WebServlet(value = {"/hello"})@ServletComponentScan

    @Configuration
    public class Myconfig {
        @Bean
        public ServletRegistrationBean servletRegistrationBean(){
            ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean();
            servletRegistrationBean.setServlet(new HelloServlet());
            servletRegistrationBean.addUrlMappings("/hello");
            return servletRegistrationBean;
        }
    

    监听器

    在app里面加入@ServletComponentScan

    @WebListener
    @Slf4j
    public class ServletListener implements ServletRequestListener {
        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            log.info("有人");
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            log.info("没人");
        }
    }
    

    使用ServletListenerRegistrationBean可以不用写@WebListener@ServletComponentScan

      @Bean
        public ServletListenerRegistrationBean servletListenerRegistrationBean(){
            ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean();
            servletListenerRegistrationBean.setListener(new ServletListener());
            servletListenerRegistrationBean.setEnabled(true);
            return servletListenerRegistrationBean;
        }

    Filter

    依赖

     <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>alibaba-dingtalk-service-sdk</artifactId>
                <version>2.0.0</version>
            </dependency>
    @Slf4j
    @WebFilter("/api/*")
    public class TokenFilter1 implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            Filter.super.init(filterConfig);
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request=  (HttpServletRequest)servletRequest;
            HttpServletResponse response=  (HttpServletResponse)servletResponse;
            String token = request.getParameter("token");
            log.info("-------TokenFilter1开始------");
            //log.info("获取到token->{}",token);
            if(StrUtil.isEmpty(token)){
                response.sendRedirect("/logout");
                return;
            }
            StopWatch sw = new StopWatch();
            sw.start();
            filterChain.doFilter(servletRequest,servletResponse);
            sw.stop();
            log.info("-------TokenFilter1结束,耗时:{}------",sw.getTotalTimeSeconds());
        }
    
        @Override
        public void destroy() {
            Filter.super.destroy();
        }
    }
    
        @Bean
        public FilterRegistrationBean filterRegistrationBean(){
            FilterRegistrationBean filter = new FilterRegistrationBean();
            filter.setFilter(new TokenFilter1());
            filter.addUrlPatterns("/api/*");
            filter.setName("filter1");
            filter.setOrder(2);
            return filter;
        }
        @Bean
        public FilterRegistrationBean logfilterRegistrationBean(){
            FilterRegistrationBean filter = new FilterRegistrationBean();
            filter.setFilter(new LogFilter2());
            filter.addUrlPatterns("/api/*");
            filter.setName("filter2");
            filter.setOrder(1);
            return filter;
        }

    logout为

    package com.beiyou.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class LogOut {
    
        @GetMapping("/logout")
        public String logout(){
    
            return "滚出去";
        }
    }

    展开全文
  • @Configuration public class MyServletRegistrationBean { @Bean public ServletRegistrationBean initServletRegistrationBean(){ ServletRegistrationBean servletServletRegistrationBean=new ...

    一、定义Servlet

    package com.example.config;
    
    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 {
            resp.getOutputStream().println("hello world...");
        }
    }
    

    二、注册Servlet

    package com.example.config;
    
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MyServletRegistrationBean {
    
        @Bean
        public ServletRegistrationBean<MyServlet> initServletRegistrationBean(){
            ServletRegistrationBean<MyServlet> servletServletRegistrationBean=new ServletRegistrationBean<>();
            servletServletRegistrationBean.setServlet(new MyServlet());
            servletServletRegistrationBean.addUrlMappings("/welcome");
            return servletServletRegistrationBean;
        }
    
    }
    

    其中servletServletRegistrationBean.addUrlMappings(“/welcome”);这句来完成地址映射。
    三、postman测试:
    在这里插入图片描述

    展开全文
  • springbootservlet注册(ServletRegistrationBean) 作用:servlet可拦截指定url路径,添加自定义操作 ************************************** 相关类 HttpServlet public abstract class HttpServlet ...

    springboot servlet注册(ServletRegistrationBean)

     

    作用:servlet可拦截指定url路径,添加自定义操作

     

     

    *********************

    相关类

     

    HttpServlet

    public abstract class HttpServlet extends GenericServlet {
    
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    

     

    ServletRegistrationBean:servlet的注册类

    public class ServletRegistrationBean<T extends Servlet> extends DynamicRegistrationBean<Dynamic> {
        private static final String[] DEFAULT_MAPPINGS = new String[]{"/*"};
        private T servlet;
        private Set<String> urlMappings;
        private boolean alwaysMapUrl;
        private int loadOnStartup;
        private MultipartConfigElement multipartConfig;
    
    ********************
    构造函数
    
        public ServletRegistrationBean() {
        public ServletRegistrationBean(T servlet, String... urlMappings) {
        public ServletRegistrationBean(T servlet, boolean alwaysMapUrl, String... urlMappings) {
    
    
    *********************
    常用方法
    
        public void setServlet(T servlet) {    //设置servlet
        public T getServlet() {                //获取servlet
    
        public void setUrlMappings(Collection<String> urlMappings) {
        public Collection<String> getUrlMappings() {
        public void addUrlMappings(String... urlMappings) {  //添加映射路径
    
        public String getServletName() {   //获取servlet的名称
    

    说明: urlappings不支持通配符,需为具体的路径

     

     

    *********************

    示例

     

    *****************

    servlet 层

     

    MyServlet

    public class MyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getOutputStream().println("hello world");
        }
    }
    

     

    *****************

    config 层

     

    WebConfig:注册servlet

    @Configuration
    public class WebConfig {
    
        @Bean
        public ServletRegistrationBean<MyServlet>  initServletRegistrationBean(){
            ServletRegistrationBean<MyServlet> servletServletRegistrationBean=new ServletRegistrationBean<>();
            servletServletRegistrationBean.setServlet(new MyServlet());
            servletServletRegistrationBean.addUrlMappings("/hello");
    
            return servletServletRegistrationBean;
        }
    
    }
    

     

     

    *********************

    测试输出

     

    localhost:8080/hello

                             

     

     

    展开全文
  • springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean
  • Spring boot 添加 Servlet(ServletRegistrationBean

    万次阅读 多人点赞 2019-04-16 10:15:59
    所以想用使用 Servlet 功能,就必须要借用 Spring Boot 提供的 ServletRegistrationBean 接口。 因此整个实现就变得很简单了。我们只需要在 @Configuration 注解过的类中做以下配置即可使用 Servlet 功能。 ...
  • 感觉ServletRegistrationBean在Springboot中是一个可以看懂的类,好像作用就相当于@Controoller注解, package org.springframework.boot.web.servlet; /** * A {@link ServletContextInitializer} to ...
  • 在SpringBoot1.4.0+以后import org.springframework.boot.context.embedded....import org.springframework.boot.context.embedded.ServletRegistrationBean;变成了import org.springframework.boot.we
  • ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new MyServlet()); servletRegistrationBean.addUrlMappings("/login","/test");// ...
  • yunguanjiacode06131049.zip

    2018-07-11 10:28:55
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //添加初始化参数:initParams //白名单: servletRegistrationBean.addInitParameter...
  • @Configuration(proxyBeanMethods = true) public class RegistrationBean { @Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean servletRegistrationBean = new ...
  • } @Bean public ServletRegistrationBean druidStatViewServlet() { //ServletRegistrationBean提供类的进行注册 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new...
  • } } 2.configuration @Configuration public class MyConfiguration { @Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean<HelloServletConfig> bean = new ...
  • Spring Boot Servlet : RegistrationBean

    千次阅读 2019-05-22 08:45:22
    概述 功能介绍 Spring Boot提供的针对Servlet 3.0+容器的注册bean基类。该类的目的是用于向Servlet容器... 这是一个抽象基类,实现了接口ServletContextInitializer。ServletContextInitializer约定了当前Registrat...
  • @SpringBootApplication public class SpringBootSimpleApplication { /** * 使用代码注册Servlet(不需要@ServletComponentScan注解) */ @Bean public ServletRegistrationBean servletRegistrationBean() { ...
  • * Return a mutable collection of the ServletRegistrationBean that the filter * will be registered against. ServletRegistrationBeans. * @return the Servlet registration beans * @see #...
  • 编译后lombok提示“找不到符号”

    千次阅读 2020-06-17 10:43:37
    import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org....
  • SpringBoot——注册Servlet三大组件

    千次阅读 2019-03-20 16:19:12
    之前我们是在web.xml中注册这三大组件,由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器,以此来启动SpringBoot的web应用,并没有web.xml文件,所以在SpringBoot中我们需要使用ServletRegistrationBean ...
  • springboot之Druid数据源

    2018-05-22 18:04:10
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //设置ip白名单 //servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //...
  • 其中需要注意的是registration.setName("rest"),这个语句很重要,因为name相同的ServletRegistrationBean只有一个会生效,也就是说,后注册的会覆盖掉name相同的ServletRegistrationBean。 如果不指定,默认为...
  • ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //添加初始化参数:initParams //白名单: servletRegistrationBean.addInitParameter(...
  • } /** * 注册Servlet信息, 配置监控视图 * @return */ @Bean @ConditionalOnMissingBean public ServletRegistrationBean druidServlet() { ServletRegistrationBean servletRegistrationBean = new ...
  • } @Bean public ServletRegistrationBean servletRegistrationBean2(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), propertyResolver.getProperty(...
  • return servletRegistrationBean; } 2、FilterRegistrationBean @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(); ...
  • //用ServletRegistrationBean包装servlet ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet); registrationBean.setLoadOnStartup(1); //指定urlmapping ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 13,171
精华内容 5,268
关键字:

servletregistrationbean