您好我正在尝试通过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();
}
}
答案 0 :(得分:1)
您需要在Servlet中导入Login
类。
这可能是由于您的Login
课程和LoginServlet
两个都在不同的包。因此,在使用之前,您需要在Login
中导入LoginServlet
课程。
您需要添加类似
的内容import yourpackage.Login;
开头的 LoginServlet
答案 1 :(得分:1)
将Login类导入LoginServlet.java。 如果两者都在同一个包中,那么你必须将它添加到类路径中。
它可能对您有所帮助。 感谢