您的位置:首页 > 新闻 > 热点要闻 > 羽毛球赛事直播平台_重庆建一个网站_网站优化排名_企业网站模板

羽毛球赛事直播平台_重庆建一个网站_网站优化排名_企业网站模板

2025/1/7 4:44:36 来源:https://blog.csdn.net/2201_76081438/article/details/144926825  浏览:    关键词:羽毛球赛事直播平台_重庆建一个网站_网站优化排名_企业网站模板
羽毛球赛事直播平台_重庆建一个网站_网站优化排名_企业网站模板

一、如何定制和修改Servlet容器的相关配置

修改和server有关的配置(ServerProperties)

server.port=8081
server.context‐path=/tx
server.tomcat.uri-encoding=UTF-8

二、注册servlet三个组件【Servlet、Filter、Listener】

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。之前这三个都是配置在web.xml中的,现在:

1.Sevlet

MyServlet

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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().print("Hello Servlet");}
}

MyServerConfig

@Configuration
public class MyServerConfig {/** 注册三大组件* *///Servlet@Beanpublic ServletRegistrationBean myServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(newMyServlet(),"/myServlet");return registrationBean;}
}

运行:

在这里插入图片描述

2.Filter

MyFilter

public class MyFilter implements Filter {public MyFilter() {super();}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("myFilter初始化...");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("MyFilter...");filterChain.doFilter(servletRequest, servletResponse);}@Overridepublic void destroy() {}
}

MyServerConfig

@Configuration
public class MyServerConfig {/** 注册三大组件* *///Filter@Beanpublic FilterRegistrationBean myFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new MyFilter());registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));return registrationBean;}}

运行:
在这里插入图片描述

在这里插入图片描述

3.Listener

MyListener

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("contextInitialized ...启动");}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("contextDestroyed ...销毁");}
}

MyServerConfig

@Configuration
public class MyServerConfig {/** 注册三大组件* *///Listener@Beanpublic ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean<MyListener> registrationBean = newServletListenerRegistrationBean<>(new MyListener());return registrationBean;}}

运行:
在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com