0

I'm getting an error when I try to log on at localhost:8080 with my code. Can someone please help me. I'm new to web application development. I've connected to a database (mysql) which contains employee_id and password columns which the JAVA application will check with to validate the login. But it is returning an error. I have attached all my files below as well as the error, thanks! I'm getting the error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

even though I've installed the connector.

Login.java (servlet)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String employee_id = request.getParameter("employee_id");
    String password = request.getParameter("password");
    if(Validate.checkUser(employee_id, password)) { 
        RequestDispatcher rs = request.getRequestDispatcher("Welcome");
        rs.forward(request, response);
    }
    else
    {
       out.println("Employee ID or Password is incorrect. Please try again.");
       RequestDispatcher rs = request.getRequestDispatcher("index.html");
       rs.include(request, response);
    }
}  


    }

Welcome.java (servlet)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Welcome extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse     response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("Welcome user");
  }  
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="login" method="post">
<h3>
Employee Login
</h3>
<b>Employee ID:</b> <br>
<input type="text"name="employee_id" size="20"><br><br>
<b>Password:</b><br>
<input type="password" name="password" size="20"><br><br>
<input type="submit" value="Login"><br><br>
</form>
</body>
</html>

Validate.java (class)

import java.sql.*;
public class Validate
{
public static boolean checkUser(String employee_id, String password)
{
    boolean st = false;
    try { 
        Class.forName("com.mysql.jdbc.Driver"); 

        Connection con =      DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll", "root", ""); 
        PreparedStatement ps = con.prepareStatement("select * from employee_login where employee_id =? and password = ?");
        ps.setString(1, employee_id);
        ps.setString(2, password);
        ResultSet rs =ps.executeQuery();
         st = rs.next();

     }catch(Exception e)
      {
          e.printStackTrace();
      }
         return st;                 
  }   
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

<servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>Login</servlet-class>
</servlet>
<servlet>
    <servlet-name>Welcome</servlet-name>
    <servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/Welcome</url-pattern>
</servlet-mapping>

</web-app>

AND I"M GETTING THIS ERROR WHEN I TRY TO LOG IN:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Validate.checkUser(Validate.java:8)
at Login.doPost(Login.java:15)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745) 
A J
  • 3,970
  • 14
  • 38
  • 53
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • Have you imported the mysql's connector jar in your java build path? – Rajnikant Patel Dec 26 '15 at 05:04
  • @RajnikantPatel Yes, I have, but it's still giving this error. I right-clicked on my project and went to Build Path -> Configure Build Path -> Add external JAR -> mysql-connector-java-5.1.38 – Programmer Dec 26 '15 at 05:06
  • 1
    If it's there than there should not be such error. Try to clean and build your project again and restart your tomcat from eclipse or command prompt. – Rajnikant Patel Dec 26 '15 at 05:16
  • @RajnikantPatel I don't understand why I'm still receiving this error. I think I've imported it correctly. I downloaded the driver from mysql website. I went to configure build path -> add external jar -> and selected the file mysql-connector-java-5.1.38-bin.jar – Programmer Dec 26 '15 at 06:18

3 Answers3

3

Check that driver package is in correct directory (under WEB-INF/lib folder) or pointed jar correctly in your build path with proper version.

Milan Kamboya
  • 486
  • 2
  • 11
  • It is not under my WEB-INF/lib folder. This folder is completely empty. Is it necessary to add it there? And if so, how do I add it? But the package is in my project's build path. And it's also visible in Java resources -> Libraries – Programmer Dec 26 '15 at 06:23
  • no. it's not necessary to add there if you have added in build path. but try this way. it may solve the issue.Just put this jar under this dir either by copying or going to the project workspace. – Milan Kamboya Dec 26 '15 at 06:28
  • 1
    YES! It worked, thank you very much! – Programmer Dec 26 '15 at 06:37
  • 1
    And thank you very much everyone else who also posted a suggestion. Much appreciated. – Programmer Dec 26 '15 at 06:38
  • welcome :). hope you are clear now. – Milan Kamboya Dec 26 '15 at 06:42
  • Yes, thanks! I have a lot to do now. This is a school project and I have to create a payroll system. I have about 8 months to do it though haha – Programmer Dec 26 '15 at 06:49
2

Class.forName("com.mysql.jdbc.Driver").newInstance();

I think you are missing newInstance there. Can you add it and try again?

Umopepisdn
  • 807
  • 6
  • 16
0

Check that your driver package is imported,and try again

prasun
  • 7,073
  • 9
  • 41
  • 59
  • I right clicked my project folder Build Path -> Configure Build Path -> Add external JAR -> mysql-connector-java-5.1.38 – Programmer Dec 26 '15 at 06:07