javamysqljsptomcatjdbcrealm

How to check if JSP is connected to a database?


I am making a login system using form login (with JSecurityCheck). The username and password is stored inside a MySQL database. When I try to login with the credentials in the database, it procs the error page. I want to see if it is even accessing the database, but I'm not sure how.

Here are the related source codes (with only the important stuff):

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 (under Apache Tomcat directory):

<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" />

============================== EDIT =================================

I played around a bit and realized I could just check from the console.

I am not too familiar with realms, so if anyone sees why it is not connecting please leave a comment/answer.

Error:

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

Solution

  • You do not have the MySql driver class present in Tomcat or your application. You can download the driver from here and place it in the "$CATALINA_HOME/lib" folder.