<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="java.sql.*" %> <%@page import="java.sql.Statement" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆跳转servlet</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%-- 获取前端数据 --%> <% String usernum = request.getParameter("usernum"); String userpwd = request.getParameter("userpwd"); %> <%-- 定义SQLConn类和INSERT_SQL常量 --%> <% SQLConn sqlConn = new SQLConn(); //final String INSERT_SQL = "INSERT INTO User_ZC(UserID, Name, Code, Power) VALUES (?, ?, ?, ?)"; sqlConn.openDB();//打开数据库 String sql = "select UserId,Code from User_ZC"; ResultSet rs = sqlConn.execQuery(sql); boolean v = false; while(rs.next()){ if(rs.getString("usernum").equals(usernum)&&rs.getString("userpwd").compareTo(userpwd)==0){ v = true; } } rs.close(); sqlConn.closeDB(); if(v){ request.getRequestDispatcher("ZCS.jsp").forward(request, response); }else{ request.getRequestDispatcher("ZCB.jsp").forward(request, response); } %> </body> </html> 找bug
时间: 2024-02-09 12:11:59 浏览: 145
23袁雨轩.docx
在代码中,有两处问题:
1. 在获取前端数据时,需要使用参数名 `usernum` 和 `userpwd` 而不是 `UserID` 和 `Code`。
```
String usernum = request.getParameter("usernum");
String userpwd = request.getParameter("userpwd");
```
2. 在比较密码时,应该使用 `equals()` 方法而不是 `compareTo()` 方法。
```
if(rs.getString("usernum").equals(usernum) && rs.getString("userpwd").equals(userpwd)) {
v = true;
}
```
修改后的代码如下所示:
```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.util.SQLConn"%>
<%@page import="java.sql.*" %>
<%@page import="java.sql.Statement" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登陆跳转servlet</title>
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body>
<%-- 获取前端数据 --%>
<%
String usernum = request.getParameter("usernum");
String userpwd = request.getParameter("userpwd");
%>
<%-- 定义SQLConn类和INSERT_SQL常量 --%>
<%
SQLConn sqlConn = new SQLConn();
sqlConn.openDB(); //打开数据库
String sql = "select UserId,Code from User_ZC";
ResultSet rs = sqlConn.execQuery(sql);
boolean v = false;
while(rs.next()){
if(rs.getString("UserId").equals(usernum) && rs.getString("Code").equals(userpwd)) {
v = true;
}
}
rs.close();
sqlConn.closeDB();
if(v){
request.getRequestDispatcher("ZCS.jsp").forward(request, response);
} else {
request.getRequestDispatcher("ZCB.jsp").forward(request, response);
}
%>
</body>
</html>
```
阅读全文