Java Error无法找到符号

时间:2014-07-09 10:35:01

标签: java jsp servlets

您好我正在尝试通过JSP获取登录信息。编译Can't find symbol : Login

时收到错误LoginServlet.java

这是我的代码,你能告诉那个问题的解决方案吗?

Login.java

 public class Login
{
public static boolean validate(String name, String pass) 
{       
    boolean status = false;
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String driver = "oracle.jdbc.driver.OracleDriver";
    String userName = "system";
    String password = "dad";
    try 
      {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url,userName, password);

        pst = conn.prepareStatement("select * from login where user=? and password=?");
        pst.setString(1, name);
        pst.setString(2, pass);

        rs = pst.executeQuery();
        status = rs.next();

    } 
catch (Exception e) 
   {
        System.out.println(e);
    } 
    finally 
        {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    return status;
}
}

LoginServlet.java

public class LoginServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;

public void do Post(HttpServletRequest request, HttpServletResponse response)throws      ServletException, IOException
    {  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  

    String n=request.getParameter("username");  
    String p=request.getParameter("userpass"); 

    HttpSession session = request.getSession(false);
    if(session!=null)
    session.setAttribute("name", n);
    session.setAttribute("pass", p);

    if(Login.validate(n,p))
    {  
        Request Dispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        rd.forward(request,response);  
    }  
    else
    {  
        out.print("<p style=\"color:red\">Sorry username or password error</p>");  
        Request Dispatcher rd=request.getRequestDispatcher("index.jsp");  
        rd.include(request,response);  
    }  

    out.close();  
}  

}

2 个答案:

答案 0 :(得分:1)

您需要在Servlet中导入Login类。

这可能是由于您的Login课程和LoginServlet两个都在不同的。因此,在使用之前,您需要在Login中导入LoginServlet课程。

您需要添加类似

的内容 import yourpackage.Login;

开头的

LoginServlet

答案 1 :(得分:1)

将Login类导入LoginServlet.java。 如果两者都在同一个包中,那么你必须将它添加到类路径中。

它可能对您有所帮助。 感谢