简体   繁体   中英

Why do I get an error when running a method from a jsp page, but not from a main method?

I'm making a Java web project in IntelliJ IDEA Ultimate. I have a class with a method that takes two Strings as input, queries a database, and returns an int value. (It's a login method - takes in username and password - if they are valid, it should return the int of the userid, otherwise 0).

I tested this method by calling it from a "Tester" class with a main method, and it returned the expected value. However, when calling this method from a jsp page, it gives get a ClassNotFound Exception for my SQL driver (com.sql.jdbc.Driver). (I know it's this method giving me the error, since I added a System.out.println to debug it)

Why do I only get this error when calling the method from my jsp page? How can I fix this?

The exact error I get in the console:

LogIn method error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

The LogIn method that's giving me pain:

public static int logIn(String username, String password) {
        try {

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

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_database", "root",
                    "password12345");
            Statement stmt = con.createStatement();
            ResultSet users = stmt.executeQuery("SELECT * FROM users");

            ArrayList<User> userList = new ArrayList<User>();
            while (users.next()) {
                userList.add(
                        new User(users.getInt("id"), users.getString("username"), users.getString("password"),
                                users.getString("full_name"),
                                users.getString("email"), users.getInt("admin") == 1));
            }
            for (int i = userList.size() - 1; i >= 0; i--) {
                if (username.equals(userList.get(i).getUserName()) && userList.get(i).testPassword(password)) {
                    System.out.println(userList.get(i).getId());
                    return userList.get(i).getId();
                }
            }
            return 0;
        } catch (Exception e) {
            System.out.println("LogIn method error " + e);
            return 0;
        }
    }

The JSP page :

<%@ page import="com.neilbanerjee.SupportMeLogic,java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    int loggedInID = MyOtherClass.logIn(request.getParameter("username"), request.getParameter("password"));
    com.neilbanerjee.User loggedIn;
    if (loggedInID == 0) {
        loggedIn = null; %>
        <%--<jsp:forward page="index.jsp"></jsp:forward>--%>
<%
    } else {
        loggedIn = MyOtherClass.loggedInUser(loggedInID);
    }
%>
<html>
<head>
    <title>SupportMeDevices</title>
</head>
<body>

Welcome, <% out.print(loggedIn.getFullName());%>!

</body>
</html>

When your web app is deployed on server it uses jars of server's lib folder and jars of your WEB-INF/lib folder.

It is recomended that you put javax.servlet.jsp.jstl-1.2.4.jar and javax.servlet.jsp.jstl-api-1.2.1.jar inside WEB-INF/lib to be able to use jstl on jsp pages.

When you call it from main method, The JDBC Driver classes are there in the classpath and Java can run the application without an error.

But when you try to access these classes from JSP, The deployed war should contain the JDBC Driver classes inside it. For deployed war file to have all the necessary classes, You need to include all the libraries your app needs into 'WEB-INF/lib'. Try adding them and re-deploy the war file and see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM