當前位置:
首頁 > 知識 > jsp登錄頁面密碼equals驗證出現問題解決辦法

jsp登錄頁面密碼equals驗證出現問題解決辦法

從資料庫中取出的密碼或者用戶名後會帶有一串的空格

所以equals比較時會顯示錯誤

使用trim()函數去除空格就可以了

運行成功代碼如下:

login.jsp

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

pageEncoding="utf-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>登錄界面</title>

</head>

<body>

<center>

<h1 stylex="color:red">登錄</h1>

<form id="indexform" name="indexForm" action="logincheck.jsp" method="post">

<table border="0">

<tr>

<td>賬號:</td>

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

</tr>

<tr>

<td>密碼:</td>

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

</td>

</tr>

</table>

<br>

<input type="submit" value="登錄" stylex="color:#BC8F8F">

</form>

</center>

</body>

</html>

logincheck.jsp

<%@page import="Bean.DBBean"%>

<%@ page import="java.sql.*" 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>

<jsp:useBean id="db" class="Bean.DBBean" scope="page"/>

<%

request.setCharacterEncoding("UTF-8");

String username=(String)request.getParameter("username");

String password=(String)request.getParameter("password");//取出login.jsp的值

//下面是資料庫操作 *代表所有值

db.getconn();

String sql="select * from lhT where username="+"""+username+"";";//定義一個查詢語句

ResultSet rs=db.executeQuery(sql);//運行上面的語句

if(rs.next())

{

/* if(password.equals(rs.getString(2)))

{

} */

Boolean c= db.com(password,rs.getString("password"));

if(c){

response.sendRedirect("loginsuccess.jsp");

}

else{

out.print("<script language="javaScript"> alert("密碼錯誤");</script>");

response.setHeader("refresh", "0;url=login.jsp");

}

}

else

{

out.print("<script language="javaScript"> alert("請輸入用戶名——else");</script>");

response.setHeader("refresh", "0;url=login.jsp");

}

%>

</body>

</html>

loginsussess.jsp

<%@ page import="java.sql.*" 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=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<h1>登錄成功 </h1>

</body>

</html>

DBBean.java

package Bean;

import java.sql.*;

public class DBBean {

private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=lhsjk";

private String dbusername = "sa";

private String dbpassword = "123456";

private Connection conn = null;

private Statement stmt = null;

public DBBean() {}

public boolean com(String a,String b)

{

a=a.trim();

b=b.replaceAll("\s*", "");

System.out.println(a);

System.out.println(b);

if(a.equals(b))

{

return true;

}

else

return false;

}

public void getconn()

{

try

{

Class.forName(driverStr);

conn = DriverManager.getConnection(connStr, dbusername, dbpassword);

stmt = conn.createStatement();

}

catch (Exception ex) {

System.out.println(ex.getMessage());

System.out.println("數據連接失敗!");

}

}

public int executeUpdate(String s) {

int result = 0;

System.out.println("--更新語句:"+s+"
");

try {

result = stmt.executeUpdate(s);

} catch (Exception ex) {

System.out.println("執行更新錯誤!");

}

return result;

}

public ResultSet executeQuery(String s) {

ResultSet rs = null;

System.out.print("--查詢語句:"+s+"
");

try {

rs = stmt.executeQuery(s);

} catch (Exception ex) {

System.out.println("?執行查詢錯誤!");

}

return rs;

}

public void execQuery(String s){

try {

stmt.executeUpdate(s);

} catch (SQLException e) {

// TODO Auto-generated catch block

System.out.println("執行插入錯誤!");

}

}

public void close() {

try {

stmt.close();

conn.close();

} catch (Exception e) {

}

}

}

文件結構目錄:

jsp登錄頁面密碼equals驗證出現問題解決辦法

打開今日頭條,查看更多圖片

---------------------

作者:冰咖啡ol

原文:https://blog.csdn.net/lyd20180717/article/details/85529831

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

消極架構!
Spring Boot 2.1.X整合最新版本Elasticsearch的相關問題

TAG:程序員小新人學習 |