0

我正在尝试使用 JSP 创建一个登录系统并连接到 MySQL 数据库,并得到以下错误: 错误信息

我的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.core.bibit.service.Security"
import="javax.servlet.*"
import="java.util.*"
import="com.mysql.jdbc.Driver"
%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

boolean test = new Security().validateUser(username, password);
System.out.println("Authen: " + test);
if(test){
    response.sendRedirect("test.jsp");
}else{
    response.sendRedirect("about.jsp");
} 
%>

package com.core.bibit.service;
import com.mysql.jdbc.Driver;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Security {

    private final static int ITERATION_NUMBER = 1000;

    public Security(){

    }

    ...

    public boolean validateUser(String userName, String password){

        boolean valid = false;

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:8889/bibit?"
                                                             + "user=root&password=root");                  

            String query = "SELECT * FROM bibit.User WHERE username = ?";

            ps = con.prepareStatement(query);
            ps.setString(1, userName);

            rs = ps.executeQuery();

            String digest, salt;

            if(rs.next()){
                digest = rs.getString("PASSWORD");
                salt = rs.getString("SALT");

                System.out.println("Digest: " + digest);
                System.out.println("Salt: " + salt);

                if(digest == null || salt == null){
                    System.out.println("IF1");
                    throw new SQLException("Database inconsistant Salt or Digested Password altered");
                }
                if(rs.next()){
                    System.out.println("IF2");
                    throw new SQLException("Database inconsistent two CREDENTIALS with the same LOGIN");
                }
                /*else{
                    System.out.println("IF3");
                    digest = "000000000000000000000000000=";
                    salt = "00000000000=";
                }*/

                byte[] bDigest = base64ToByte(digest);
                byte[] bSalt = base64ToByte(salt);

                //compute digest
                byte[] proposedDigest = getHash(ITERATION_NUMBER, password, bSalt);
                System.out.println("bDigest: " + bDigest);
                System.out.println("proDigest: " + proposedDigest);

                System.out.println("Response: " +     Arrays.equals(proposedDigest, bDigest)); // && valid)
                valid = Arrays.equals(proposedDigest, bDigest);
            }




        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
            //  rs.close();
                //ps.close();
                //con.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return valid;

        }
    }

}

我使用 TomEE / Tomcat 7.0,我的 MySQL 库是:mysql-connector-java-5.1,我尝试将其添加到 server.xml 文件中:

<Resource name="jdbc/DATABASENAME" type="javax.sql.DataSource" maxActive="4" maxIdle="2"    username="root" password="" maxWait="23000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:8889/bibit"></Resource>

任何人都可以解释什么是错的,也许如何解决它,或者只是朝着正确的方向推进?

解决方案:如果通过 Eclipse 启动服务器,则通过 Eclipse 添加 mysql 连接器并在一行中导入文件,如下所示

4

2 回答 2

2

例如,您需要使用逗号分隔的导入列表

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.core.bibit.service.Security, javax.servlet.*,java.util.*, com.mysql.jdbc.Driver"%>

并确保您在类路径中有所需的 jars/类(在运行时也是如此)

于 2013-10-14T07:19:11.790 回答
0

您的 JSP 代码不需要所有这些导入,除了Security类,删除所有其他

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.core.bibit.service.Security" %> //only Security class is needed

检查方法getConnection

public static Connection getConnection(String url, 
                                       String user, 
                                       String password) throws SQLException

尝试建立到给定数据库 URL 的连接。DriverManager尝试从已注册的 JDBC 驱动程序集中选择适当的驱动程序。

参数:

  • url - 形式为 jdbc:subprotocol:subname 的数据库 url
  • user - 代表其建立连接的数据库用户
  • 密码 - 用户的密码

确保所有必需的 jar 都在类路径中

于 2013-10-14T07:33:51.753 回答