问答详情
源自:4-11 阶段案例——实现用户登录

为什么无论用户名和密码一样不一样都只能登陆到失败界面呢?

<<login.jsp>>代码

<%@ 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>Insert title here</title>

</head>

<body>

<form action="dologin.jsp">

用户名:

<input type = "text" name="name"/>

<br>

密码:

<input type="password"name="pass"/> 

<br>

提交:

<input type="submit" value="提交"/>

</form>

</body>

</html>


<<dologin.jsp>>代码

<%@ 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>Insert title here</title>

</head>

<body>

<%String uname = request.getParameter("name"); 

String pwd = request.getParameter("pass");

if("admin".equals(uname)&&"admin".equals("pwd")){


request.getRequestDispatcher("login_success.jsp").forward(request, response);

}

else{

session.setAttribute("loginuser"," admin");

response.sendRedirect("login_default.jsp");

}

 

 

 


%>







</body>

</html>

登陆成功界面代码

<%@ 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>Insert title here</title>

</head>

<body>

<h3 align="center">登录成功</h3>

<p align="center">欢迎你</p>

</body>

</html>

登陆失败代码

<%@ 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>Insert title here</title>

</head>

<body>

<h3 align="center">登录失败<font color="red"><%=session.getAttribute("loginuser") %></font>请检查用户名和密码</h3>

<p align="center"><a href="login.jsp">返回登录界面</a></p>

</body>

</html>


提问者:qq_散一地寂寞_04176039 2017-10-03 11:14

个回答

  • qq_散一地寂寞_04176039
    2017-10-03 11:27:22

    感谢各位,问题解决了。我是在equals后的括号内加了引号。同学们引以为鉴。