如何检查 JSP 是否连接到数据库?

How to check if JSP is connected to a database?

我正在制作一个使用表单登录的登录系统(JSecurityCheck)。用户名和密码存储在 MySQL 数据库中。当我尝试使用数据库中的凭据登录时,它会处理错误页面。我想看看它是否正在访问数据库,但我不确定如何访问。

这里是相关的源代码(只有重要的东西):

web.xml:

<security-constraint>
    <display-name>Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>Protected Area</web-resource-name>
        <url-pattern>/protected/*</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name> manager </role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

<!-- Default login configuration uses form-based authentication -->
<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Example Form-Based Authentication Area</realm-name>
    <form-login-config>
        <form-login-page>/home.jsp</form-login-page>
        <form-error-page>/error.jsp</form-error-page>
    </form-login-config>
</login-config>
<security-role>
    <description> An administrator </description>
    <role-name> manager </role-name>
</security-role>

login.jsp:

<html>
<head>
<title>Login to Employee Services</title>
<body bgcolor="white">
    <br /><br />
    <form method="POST"
        action='<%=response.encodeURL("j_security_check")%>'>
        <table border="1">
            <thead>
                <tr>
                    <th colspan="2">Employee Services</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Employee Number</td>
                    <td><input type="text" name="j_username" required /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="j_password" required /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit"
                        value="Log In" /> &nbsp;&nbsp; <input type="reset" value="Reset" />
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

server.xml(在 Apache Tomcat 目录下):

<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
        driverName="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/UserDB?user=root;password=root"
        userTable="users" userNameCol="employeeNum" userCredCol="password"
        userRoleTable="user_roles" roleNameCol="role" />

==============================编辑============== ===================

我试了一下,意识到我可以从控制台进行检查。

我对领域不太熟悉,所以如果有人看到它为什么没有连接,请留下 comment/answer。

错误:

SEVERE: Exception performing authentication
java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:692)
at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:350)
at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:294)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:449)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:688)
... 15 more

您的 MySql 驱动程序 class 不存在于 Tomcat 或您的应用程序中。 您可以从 here 下载驱动程序并将其放在“$CATALINA_HOME/lib”文件夹中。