配置嵌入式服务器

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

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

properties 复制代码
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

java 复制代码
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 {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("Hello Servlet");
    }
}

MyServerConfig

java 复制代码
@Configuration
public class MyServerConfig {

    /*
    * 注册三大组件
    * */
    //Servlet
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new
                MyServlet(),"/myServlet");
        return registrationBean;
    }
}

运行:

2.Filter

MyFilter

java 复制代码
public class MyFilter implements Filter {
    public MyFilter() {
        super();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myFilter初始化...");
    }

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

    @Override
    public void destroy() {

    }
}

MyServerConfig

java 复制代码
@Configuration
public class MyServerConfig {

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

}

运行:

3.Listener

MyListener

java 复制代码
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("contextInitialized ...启动");
    }

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

MyServerConfig

java 复制代码
@Configuration
public class MyServerConfig {

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

}

运行:

相关推荐
lilye6610 分钟前
程序化广告行业(55/89):DMP与DSP对接及数据统计原理剖析
java·服务器·前端
SKYDROID云卓小助手1 小时前
三轴云台之相机技术篇
运维·服务器·网络·数码相机·音视频
东方佑1 小时前
自动调整PPT文本框内容:防止溢出并智能截断文本
linux·运维·powerpoint
泥土编程3 小时前
kubekey -实现懒人一键部署K8S集群
linux·运维
wirepuller_king6 小时前
创建Linux虚拟环境并远程连接,finalshell自定义壁纸
linux·运维·服务器
Yan-英杰6 小时前
【百日精通JAVA | SQL篇 | 第二篇】数据库操作
服务器·数据库·sql
在野靡生.7 小时前
Ansible(1)—— Ansible 概述
linux·运维·ansible
风123456789~7 小时前
【Linux运维】查询指定日期的上月
linux·运维·服务器
zyk_5207 小时前
Docker desktop如何汉化
运维·docker·容器
韭菜盖饭7 小时前
解决Docker端口映射后外网无法访问的问题
运维·docker·容器