Server Tomcat v8.0 Server at localhost failed to start.
1.过滤器
(1)Filter 运行在服务器上,servlet2.3 增加的功能,位于客户端与服务器上web组件(servlet jsp html)之间,用于拦截请求和响应的.
拦截请求:预处理,可以对请求做相关的修改(转发,重定向到其他资源,修改请求头)
拦截响应:后处理,servlet响应的信息做修改(修改响应头)
(2)过滤器的实现主要依靠以下核心接口:
javax.servlet.Filter接口 (常用)
init() doFilter(servletRequest,ServletResponse,FilterChain) destroy()
javax.servlet.FilterConfig接口
javax.servlet.FilterChain接口
(3)编写过滤器的过程
a.创建一个类实现Filter接口
b.配置过滤器 (web.xml, 注解配置)
c.运行
public class TestFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("---init---初始化方法----------");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("-----------这是doFilter 过滤方法------------");
chain.doFilter(request, response);//放行
}
@Override
public void destroy() {
System.out.println("-------destroy销毁方法---------");
}
}
1. 配置文件:web.xml
//注册过滤器
hello //注册过滤器名称
lesson7.TestFilter //指定类
//设置过滤器的映射
hello //指定过滤器名称
杠* //设置要过滤的请求url(可以精确匹配,也可以模糊匹配)
2.采用注解的方式
@WebFilter("/*")
public class TestFilter2 implements Filter {
private int count;
public TestFilter2() {
count=6000;
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("**************这是filter2中的方法***********");
count++;
request.setAttribute("cut", count);
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
//两种配置方式
@WebFilter(value={"/*"},
initParams={@WebInitParam(name="count",value="6000")},
asyncSupported=true,
filterName="hello")
二种配置的加载顺序:先加载web.xml的, web.xml 按照配置顺序加载
再注解配置,按照过滤器名字的字符顺序加载
2.过滤器的应用
(1)字符编码
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//请求编码格式
request.setCharacterEncoding("utf8");
chain.doFilter(request, response);
//响应编码格式
response.setCharacterEncoding("utf8");
}
(2) 用户访问权限
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//验证用户是否登录过
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession sess=req.getSession();
UserInfo user=(UserInfo)sess.getAttribute("userInfo");
if(user==null){//没有登录过,重定向到登录页
res.sendRedirect("login.jsp");
}else{
chain.doFilter(request, response);//放行
}
}
优化:编码设置:注解
@WebFilter(value={"/*"},initParams={@WebInitParam(name="encoding",value="utf8")})
public class Encoding implements Filter {
private String encode;
public Encoding() {
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
encode=fConfig.getInitParameter("encoding");
}
}
// 采用web.xml配置
public class enCodingFilter2 implements Filter {
private String encode;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encode=filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
@Override
public void destroy() {
}
}
//用户访问权限:未登录请求转发login.jsp, 或者重定向login.jsp
@WebFilter("/*")
public class CheckRoleFilter implements Filter {
public CheckRoleFilter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
(3)验证用户是否登录过
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession sess=req.getSession();
UserInfo user=(UserInfo)sess.getAttribute("userInfo");
String requestPath = req.getServletPath();
if(user==null){//没有登录过,重定向到登录页 两次请求 请求转发
//req.getRequestDispatcher("login.jsp").forward(request, response); //地址没变化
//用户访问login.jsp页面放行
if(req.getRequestURI().endsWith("login.jsp")){ //此处不判断,使用重定向会进入死循环状态。
chain.doFilter(request, response);//放行
}else{
res.sendRedirect("login.jsp"); //地址有变化
}
}else{
chain.doFilter(request, response);//放行
}
}
public void init(FilterConfig fConfig) throws ServletException {
}
}