首页>代码>jsp+servlet开发java web员工客户后台管理系统>/hotel/src/com/wrq/filter/SessionFilter.java
package com.wrq.filter;

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * Servlet Filter implementation class Session
 */

public class SessionFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		//把request转换成咱们常用的request
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		HttpSession session = request.getSession();
		//获取访问路径
		String path = request.getServletPath();
		//验证session排除登录页面和存session页面
		if(session.getAttribute("userMap")==null&&!path.endsWith("login.jsp")&&!path.endsWith("LoginServlet.do")){
			//遵循html语言把页面编码改为UTF-8
			response.setContentType("text/html;charset=UTF-8");
			//获取out对象
			PrintWriter out = response.getWriter();
			//通过js提示用户并跳转页面
			out.write("<script>alert('请登录!');window.top.location.href='login.jsp'</script>");
		}else {
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
最近下载更多
pangzhihui  LV12 2023年12月25日
cyz521213  LV1 2023年12月9日
jntm1d5  LV2 2023年9月21日
你爹正在加载中  LV4 2023年7月15日
hongdongdong  LV12 2023年6月16日
1755754535  LV5 2022年12月27日
苏柒  LV10 2022年11月22日
jdczuidaima  LV16 2022年11月4日
zzzsy1  LV3 2022年7月28日
huwenxuan  LV6 2022年6月26日
最近浏览更多
阿凡达  LV9 1月29日
lvllvl  LV4 1月14日
2131234536546  LV7 1月4日
wkq888  LV16 2023年12月27日
asddwh  LV12 2023年12月26日
pangzhihui  LV12 2023年12月25日
微信网友_6786215447367680  LV4 2023年12月23日
langao 2023年12月19日
暂无贡献等级
huangzy  LV12 2023年12月13日
cyz521213  LV1 2023年12月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友