为什么我输入指定的用户名和密码后,总是无法跳转到登录成功的页面,只能到登录失败的页面?

来源:4-11 阶段案例——实现用户登录

陈婉

2016-09-03 19:20

代码如下:

login.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'login.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->


  </head>

  

  <body>

    <div id="container">

    <div class="logo">

    <a href="#"><img src="assets/logo.png" alt=""></a>

    </div>

    <div id="box">

    <form action="dologin.jsp" method="post">

    <p class="main">

    <label>用户名:</label>

    <input name="uername" value="">

    <label>密码:</label>

    <input name="password" type="password" value="">

    </p>

    <p class="space">

    <input type="submit" value="登录" class="login" style="cursor:pointer; ">

    </p>

    </form>

    </div>

    </div>

  </body>

</html>


dologin.jsp

<%@

 page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 String username="";

 String password="";

 request.setCharacterEncoding("utf-8");

 

 username=request.getParameter("username");

 password=request.getParameter("password");

 //username=request.getAttribute("username").toString();

 //password=request.getAttribute("password").toString();

 

 //如果用户名和密码都等于admin,则登录成功

 if("abc".equals(username)&&"12345".equals(password))

 {

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

 

 }

 else

 {

  response.sendRedirect("login_failure.jsp");

 }

%>


login_success.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'login.jsp' starting page</title>

    

<%--

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

--%>

<link type="text/css" href="css/login.css" rel="stylesheet"/>

<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel=""/>

<script type="text/javascript" src="js/jquery 1.3.2.min.js"></script>

<script type="text/javascript" src="js/easyTooltip.js"></script>

<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->


  </head>

  

  <body>

    <div id="container">

    <div class="logo">

    <a href="#"><img src="assets/logo.png"></a>

    </div>

    <div id="box">

    欢迎您XXX,登录成功!

    <%--<font color="red"> --%>

    </div>

    </div>

  </body>

</html>



login_failure.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'login.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->


  </head>

  

  <body>

    <div id="container">

    <div class="logo">

    <a href="#"><img src="assets/logo.png"></a>

    </div>

    <div id="box">

    登录失败!请检查用户名或者密码!<br>

    <a href="login.jsp">返回登录</a>

   

    </div>

    </div>

  </body>

</html>


写回答 关注

3回答

  • 油金哇卡呀酷咧
    2016-09-09 17:02:42

    你的input的username写错了   不是urename

  • 看那小伙3901060
    2016-09-09 10:22:19

    是不是不输用户名就会报错?记得加验证。

  • 我吃西瓜很快
    2016-09-03 19:31:53

    为何不out.write一下你获取到的username和password呢

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248278 学习 · 3071 问题

查看课程

相似问题