不知怎么的,就出密码错误,求解!谢谢了。
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312" 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>登录中</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String connStr = "jdbc:sqlserver://localhost:1433;DatabaseName=user"; String dbusername = "sa"; String dbpassword = "4980"; Connection conn = null; Statement stmt = null; ResultSet rs = null; Class.forName(driverStr); conn = DriverManager.getConnection(connStr, dbusername, dbpassword); stmt = conn.createStatement(); String username=(String)request.getParameter("username"); String password=(String)request.getParameter("password");//取出login.jsp的值 String sql="select * from login where username="+"'"+username+"'";//定义一个查询语句 rs = stmt.executeQuery(sql); if(rs.next()) { if(password.equals(rs.getObject("password"))){ response.sendRedirect("login_success.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"); } stmt.close(); conn.close(); %> </body> </html>
if(password.equals(rs.getObject("password"))){
其中rs.getObject("password")是Object类型的,需要强转为String类型。
已解决该问题