jsp实现登录验证的过滤器(jsp验证用户名和密码怎么输入)学到了

随心笔谈2年前发布 编辑
152 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买


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

@WebFilter(filterName=”loginFilter”, urlPatterns={ “/*” })
public class LoginFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse) response;
HttpSession session=req.getSession(true);
resp.setContentType(“text/html;”);
resp.setCharacterEncoding(“utf-8”);
PrintWriter out=resp.getWriter();
String request_uri=req.getRequestURI();
String ctxPath=req.getContextPath();
String uri=request_uri.substring(ctxPath.length());
if (uri.contains(“login.jsp”) || uri.contains(“loginServlet”)) {
chain.doFilter(request, response);
} else {
if (session.getAttribute(“user”) !=null) {
chain.doFilter(request, response);
} else {
out.println(“您没有登录,请先登录!3秒后回到登录页面。”);
resp.setHeader(“refresh”, “3;url=” + ctxPath + “/login.jsp”);
return;
}
}
}

public void init(FilterConfig fConfig) throws ServletException {
}

}

© 版权声明

相关文章