java 登录拦截器

Posted 方少520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 登录拦截器相关的知识,希望对你有一定的参考价值。

首先创建一个FilterServlet类  下面代码

 

package com.group.users.util;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group.users.entity.Users;


public class FilterServlet implements Filter {

public void destroy() {

}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

HttpSession session = request.getSession();

Users user = (Users) session.getAttribute("users");
if(user!=null){
chain.doFilter(request, response);
}else{
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}

public void init(FilterConfig filterConfig) throws ServletException {

}

}

 

 

 

下面wed代码

<filter>
<filter-name>FilterServlet</filter-name>
<filter-class>com.group.users.util.FilterServlet</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterServlet</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>

以上是关于java 登录拦截器的主要内容,如果未能解决你的问题,请参考以下文章

[Java]利用拦截器和自定义注解做登录以及权限验证

JAVA SSM拦截器是啥?

java用户管理 新增与登录功能及拦截器和session的使用

java 拦截器

struts2学习自定义拦截器-登录验证拦截器

Java Web学习Java拦截器