我已经创建了一个Servlet文件'LoginServlet.java'和一个Bean文件'SimpleBean.java'并将它们放置在同一个文件夹中。当我编译Bean文件时,它已成功编译,但是当我编译该Servlet文件时,出现错误“找不到”符号”,它表示我在servlet类中实例化的bean类。

LoginServlet.java

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;

public class LoginServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/writer");
        PrintWriter pw = response.getWriter();
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        SimpleBean bean = new SimpleBean();
        //admin login
        if (name.equals("admin") && password.equals("admin")) {
            RequestDispatcher rd = request.getRequestDispatcher("adminservlet");
            rd.forward(request, response);
        } else { //general login
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/" + code, "root", "");
                PreparedStatement pst = con.prepareStatement("SELECT * FROM demo_teacher WHERE name = ?");
                pst.setString(1, name);
                ResultSet rs = pst.executeQuery();
                if (rs.next()) {
                    HttpSession session = request.getSession(true);
                    session.setAttribute("user", name);
                    ArrayList rows = new ArrayList();
                    do {
                        List row = new ArrayList();
                        row.add(rs.getString("name"));
                        row.add(rs.getString("login_time"));
                        row.add(rs.getString("logout_time"));
                        rows.add(row);
                    } while (rs.next());
                    request.setAttribute("resultSet", rows);
                    RequestDispatcher rd = request.getRequestDispatcher("profile.jsp");
                    rd.forward(request, response);
                } else {
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}


SimpleBean.java

public class SimpleBean {

    String name;
    String loginTime;
    String logoutTime;

    public void setName(String n) {
        name = n;
    }

    public void setLoginTime(String t) {
        loginTime = t;
    }

    public void setLogoutTime(String t2) {
        logoutTime = t2;
    }

    public String getName() {
        return name;
    }

    public String getLoginTime() {
        return loginTime;
    }

    public String getLogoutTime() {
        return logoutTime;
    }
}


命令提示符:
java - Java Servlet类找不到bean类-LMLPHP

最佳答案

由于您是从命令行编译的,因此请检查您的类路径。确保它包含存储.class文件的目录(在这种情况下,它是当前目录):

set CLASSPATH=.;%CLASSPATH%

09-08 10:57