加了权限过滤器后,为什么会一直停在登陆界面 ,输入正确的用户名和密码不跳转;

等dai超时 Servlet 过滤器 最后由 Evons 于2019年07月22日回复

  • 3 回答
  • 1.7k 浏览
package com.jikexueyuan.filter;



public class PermissionFilter implements Filter {

    /**
     * Default constructor. 
     */
    public PermissionFilter() {
    }

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {

	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse) response;
		
		String servletPath=req.getServletPath();
		
		HttpSession session=req.getSession();
		String flag=(String)session.getAttribute("flag");
		if(servletPath !=null
				&&(servletPath.equals("/17/login.jsp")||(servletPath.equals("/17/index.jsp"))||(servletPath.equals("/17/loginservlet.jsp")))){
			chain.doFilter(request, response);
		}else{
			if(flag!=null&&flag.equals("login_success")){
				chain.doFilter(request, response);
			}else if(flag!=null&&flag.equals("login_error")){
				req.setAttribute("msg", "登陆失败,请重新登陆!!!<br/>");
				req.setAttribute("return_uri", servletPath);
				RequestDispatcher rd=req.getRequestDispatcher("/17/login.jsp");
				rd.forward(req, resp);
			}else{
				req.setAttribute("msg", "您尚未登陆!!!");
				req.setAttribute("return_uri", servletPath);
				RequestDispatcher rd=req.getRequestDispatcher("/17/login.jsp");
				rd.forward(req, resp);
			}
		}
		
	}


	
	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

}



package com.jikexueyuan.servlet;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname=request.getParameter("uname");
		String passwd=request.getParameter("upwd");
		String returnUri=request.getParameter("return_uri");
		System.out.println("用户名==》" uname);
		System.out.println("密码==> passwd");
		System.out.println("return uri==>" returnUri);
		
		RequestDispatcher rd=null;
		if(uname==null||passwd==null){
			request.setAttribute("msg", "用户名或者密码为空!!!");
			rd=request.getRequestDispatcher("/17/login.jsp");
			rd.forward(request, response);
		}else{
			if(uname.equals("1")&&passwd.equals("1")){
				request.getSession().setAttribute("flag", "login_success");
				if(returnUri !=null){
					rd=request.getRequestDispatcher(returnUri);
					rd.forward(request,response);
				}
				else{
					rd=request.getRequestDispatcher("/17/index.jsp");
					rd.forward(request,response);
				}
			}else{
				request.getSession().setAttribute("flag", "login_error");
				request.setAttribute("msg","用户名或密码输入错误!!!!!");
				rd=request.getRequestDispatcher("/17/index.jsp");
				rd.forward(request, response);
			}
		}
		
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆页面</title>


</head>
<body>
	<form action="<%=request.getContextPath()%>/loginServlet17" method="post" name="loginForm">
		<%if(request.getAttribute("return_uri")!=null ){%>
			<input type="hidden" name="return_uri" value="<%=request.getAttribute("return_uri") %>">
		<%} %>
		<table border="1" cellspacing="0" cellpadding="5" bordercolor="silver" align="center">
			<tr>
				<td colspan="2" align="center" bgcolor="#e8e8e8">用户登陆</td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="uname" /></td>
			</tr><tr>
				<td>密码:</td>
				<td><input type="password" name="upwd" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" name="submit" value="提交 "  />
					<input type="reset" name="reset" />
				</td>
			</tr>
		</table>
	</form>

<% 
		Object obj=request.getAttribute("msg");
		if(obj !=null){
			out.println(obj.toString());
		}else{
			out.println("无");
		}
	%>
</body>
</html>
  • qq_5verd2nm 2017年04月24日 回答 #2楼
  • 刚我也遇到这个问题并修复好了,其实是PermissionFilter中的doFilter()方法中

    ||(servletPath.equals("/17/loginservlet.jsp")
    路径应该改为||(servletPath.equals("/loginservlet")这样就可以了,仔细对比一下视频代码。
  • 1 评论
  • Evons 2019年07月22日 回答 #3楼
  • 楼上的不全对!注意区分对应的loginservlet17

    ||(servletPath.equals("/loginservlet")
    应该改成:
    ||(servletPath.equals("/loginservlet17")
    
  • 0 评论