SpringBoot中有内置的servlet容器
一、如何指定和修改Servlet容器的相关配置
可以在application.properties中修改servlet容器的配置
// 通用的Servlet容器设置 server.xxx
server.port=8080
server.servlet.context-path=/me
// Tomcat的设置 server.tomcat.xxx
server.tomcat.uri-encoding=utf-8
二、怎样自定义servlet、filter、listener组件
主要分为两步:
1、编写自定义组件
2、注册组件
- 1.1自定义servlet
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello servlet");
}
}
- 1.2自定义filter
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化filter");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入filter。。。");
filterChain.doFilter(servletRequest,servletResponse);//进入下一个filter
}
@Override
public void destroy() {
System.out.println("销毁filter");
}
}
- 1.3自定义listener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听器已启动。。。");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("当前监听器已销毁");
}
}
- 2.1注册自定义的组件
主要应用到的组件是:
ServletRegistrationBean
FilterRegistrationBean
ServletListenerRegistrationBean
import com.mystudy.springboot.config.filter.MyFilter;
import com.mystudy.springboot.config.listener.MyListener;
import com.mystudy.springboot.config.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import java.util.Arrays;
import java.util.EventListener;
@Configuration
public class MyServer {
// 注册自定义servlet
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet()); // 添加自定义的servlet
// 设置自定义servlet的访问路径
servletRegistrationBean.setUrlMappings(Arrays.asList("/myServlet"));
return servletRegistrationBean;
}
// 注册自定义filter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new MyFilter());// 添加自定义的filter
// 指定需要进行过滤的请求路径
filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return filterFilterRegistrationBean;
}
// 注册自定义监听器
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<EventListener> eventListener = new ServletListenerRegistrationBean<>();
eventListener.setListener(new MyListener());// 添加自定义的listener
return eventListener;
}
}
网友评论