配置嵌入式服务器

一、如何定制和修改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;
    }

}

运行:

相关推荐
小汤猿人类23 分钟前
Spring AMQP ----消息转换器
服务器·python·spring
hwscom1 小时前
如何阻止黑客扫描网站漏洞和服务器漏洞
服务器·安全·web安全
烦躁的大鼻嘎1 小时前
Linux 编程入门:打造你的首个动态进度条
linux·运维·服务器
WeeJot嵌入式1 小时前
【Linux】Linux命令
linux·运维·服务器
迷茫运维路1 小时前
Openssl1.1.1s rpm包构建与升级
运维·openssl·rpmbuild
Cikiss1 小时前
SpringMVC解析
java·服务器·后端·mvc
wdmx2 小时前
简述Linux的信号处理
linux·服务器·信号处理
King's King2 小时前
自动化立体库安全使用管理制度完整版
运维·自动化
DX_水位流量监测3 小时前
水库水位监测系统的自动化功能:减少人工干预,可实现实时监控
运维·前端·人工智能·自动化·制造·信息与通信·零售
大霞上仙3 小时前
jenkins入门5 Manage Jenkins
运维·jenkins