简体   繁体   中英

Java Servlet class can not find the bean class

I have created a servlet file 'LoginServlet.java' and a bean file 'SimpleBean.java' and placed them in a same folder.When i compiled the bean file it compiled successfully but when i compile the servlet file i get error 'cannot find symbol' and it indicates the bean class which i instantiated inside the servlet class.

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;
    }
}

Command Prompt: 命令提示符

Since you are compiling from the command line, review your classpath. Ensure it contains the directory where the .class files are being stored (in this case, it is the current directory):

set CLASSPATH=.;%CLASSPATH%

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