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

丫丫dejava 发布于 2014/11/30 12:27
阅读 3K+
收藏 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
A
AlineGuan
可能字符乱码了 调试跟踪下
开源123321
开源123321
回复 @丫丫dejava : 你好!我现在也出现了同样的问题,请问你是怎么解决的呢?
静心天涯
静心天涯
回复 @丫丫dejava : 做出来了。那之前是因为什么而不成功的?字符乱码吗?
丫丫dejava
丫丫dejava
谢谢你啊,我已经做出来了0
返回顶部
顶部