关于登陆时输入正确的用户名和密码但是提示用户密码不对的问题

丫丫dejava 发布于 2014/11/30 12:31
阅读 304
收藏 0

由于我是刚学java,对代码不太精通,所以看不出哪里出了问题,麻烦各位大神帮忙看看,谢谢~

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>登陆页面</title>
</head>
<body>
<center> 登陆界面 </center>

<center>
<form action="LoginServlet" method="post">
用户名 <input type="text" name = "mname" /> <br><br>&nbsp;&nbsp;
密码&nbsp; <input type="password" name = "mpass"> <br>
<input type = "submit" value = "提交">
</form>
</center>


</body>
</html>

 

 

welcom.jsp

 

<%@ page import ="java.util.*" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>登陆成功</title>

</head>
<body>
欢迎你!登陆成功!<br>


</body>
</html>

 

 

back.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>登陆失败</title>
</head>
<body>
用户密码错误,单击
<a href="login.jsp">这里</a>
返回
</body>
</html>

 

DBUtil.java

 

package javabean;

import java.sql.*;

public class DBUtil {
boolean bInited = false;
//加载驱动
public void initJDBC() throws ClassNotFoundException {
//加载MYSQL JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Success loading Mysql Driver!");

}

public Connection getConnection() throws ClassNotFoundException,
SQLException{
if(!bInited){
initJDBC();
}
//连接URL为 jdbc:mysql//服务器地址/数据库名
//后面的2个参数分别是登陆用户名和密码
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mybook","root","1111");
return conn;
}


public boolean loginSuccess(String mname,String mpass){
boolean returnValue = false;
String sql = "SELECT mname,mpass FROM guanli";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try{
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
String MnameInDB = rs.getString("mname");
String MpassInDB = rs.getString("mpass");
if(MnameInDB.equals(mname) &&
MpassInDB.equals(mpass)){
returnValue = true;
break;
}
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}

return returnValue;

}}

 

 

LoginServlet.jsp

 

package servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javabean.DBUtil;

public class LoginServlet implements javax.servlet.Servlet{

public void destroy() {

}

public ServletConfig getServletConfig() {
return null;
}

public String getServletInfo() {
return null;
}

public void init(ServletConfig arg0) throws ServletException {

}

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String Mname = request.getParameter("mname");//取得用户名
String Mpass = request.getParameter("mpass");//取得密码
DBUtil db = new DBUtil();//构建数据库对象
boolean canLogin = db.loginSuccess(Mname, Mpass);
if(canLogin){//根据登陆情况,跳转页面
response.sendRedirect("welcome.jsp");
}else{
response.sendRedirect("back.jsp");
}
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest)request;
HttpServletResponse rs = (HttpServletResponse) response;
doPost(rq,rs);
}

}

 

web.xml配置

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>LoginServlet</servlet-name>

<servlet-class>servlet.LoginServlet</servlet-class>

</servlet>

 

<servlet-mapping>

<servlet-name>LoginServlet</servlet-name>

<url-pattern>/login</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>login.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

加载中
0
你来人间一趟
你来人间一趟
在loginsucess里debug下不就行了,但是为什么你要写个循环呢?直接用条件去查不是更好?另外。。照片中是你本人吗
0
plugin
plugin
粘贴代码的时候,可以选择格式。
0
buglife
buglife

贴代码时贴重点部分就可以了,另外验证帐号是否正确  直接用where语句条件查询不就完了  何必将整个表的数据都搞出来,遍历结果集。

针对你当前这个问题  你应该debug你那个while语句  

0
v逍遥一生
v逍遥一生
loginSuccess方法中肯定有异常被捕获,returnValue赋值语句,没有执行到。
返回顶部
顶部