SpringBoot学习笔记
一.springBoot整合servlet
方式一:利用注解
package com.synda.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(name="firstServlet",urlPatterns = {});//可传入String数组
@WebServlet(name="firstServlet",urlPatterns = "/first")
public class firstServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget方法");
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
super.service(arg0, arg1);
}
}
方式二:利用方法注册Servlet
(1)先写一个Servlet继承HttpServlet
package com.synda.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 secondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("secondServlet");
super.doGet(req, resp);
}
}
(2)在启动类中写如下方法
@Bean
public ServletRegistrationBean getServletRegistrationBean() {
ServletRegistrationBean servletBean= new ServletRegistrationBean(new firstServlet());
servletBean.addUrlMappings("/*");
return servletBean;
}
二、springBoot整合Filter
方式一:利用注解
package com.synda.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
//@WebFilter(filterName = "firstFilter",urlPatterns = {"*.do","*.jsp"});//可传入String数组
@WebFilter(filterName ="firstFilter",urlPatterns = "/first")
public class firstFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入filter");
chain.doFilter(request, response);
System.out.println("离开filter");
}
}
方式二:利用方法注册
(1)、先写Filter
package com.synda.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class secondFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入secondFilter");
chain.doFilter(request, response);
System.out.println("离开secondFilter");
}
}
(2):启动类中编写方法
@Bean
public FilterRegistrationBean getFilterRegistrationBean() {
FilterRegistrationBean filterBean= new FilterRegistrationBean(new secondFilter());
filterBean.addUrlPatterns("/second");
return filterBean;
}