在spring-boot中定义servlet, filter, listener有两种方法:一种基于java代码,一种基于注解
以servlet为例,filter, listener等同;
首先来看基于代码的配置方式:
在程序的入口SpringBootDemoApplication.java中通过代码注册一个servlet,一定要注意加上@Bean注解,楼主在这上边吃了不少亏。
package com.lzl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.lzl.servlet.MyServlet;
@SpringBootApplication
public class SpringBootDemoApplication {
/**
* @author XXX
* @date 2017年3月7日 上午8:08:58
* @function 注册一个servlet
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
// TODO Auto-generated method stub //myServlet/* 为访问servlet的路径
return new ServletRegistrationBean(new MyServlet(), "/myServlet/*");
}
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
然后自定义MyServlet
package com.lzl.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setAttribute("message", "-------执行MyServlet。。。。--------");
request.getRequestDispatcher("/WEB-INF/view/test.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
完事以后重启服务器,访问localhost:8080/myServlet/可以顺利访问servlet
基于注解的配置方式:
基于注解要比基于代码的简单的多,楼主也推荐此种用法;
直接在程序的入口SpringBootDemoApplication.java中加入注解@ServletComponentScan
package com.lzl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.lzl.servlet.MyServlet;
@SpringBootApplication
@ServletComponentScan
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
然后定义MyServlet并加入@WebServlet注解表示此bean为一个servlet
package com.lzl.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/myServlet/*", description="这是我的第一个servlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setAttribute("message", "-------执行MyServlet。。。。--------");
request.getRequestDispatcher("/WEB-INF/view/test.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
然后重启服务器即可!
接下来filter和listener的定义,基于java代码的在这里就不多说了,和servlet差不多,只不过是ServletRegistrationBean换为FilterRegistrationBean与ServletListenerRegistrationBean
重点看下基于注解的:
package com.lzl.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(filterName="myFileter", urlPatterns="/*")
public class MyFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.err.println("--------程序正在执行过滤任务......---------");
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
package com.lzl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.err.println("----------初始化监听器.....---------------");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}