我尝试了一些变体,并且我当然导入了包含我在Servlet中使用的布尔方法的类,但无论如何它都看不到该方法。
这是我的班级登录方法:
MyCon = Database.getConnection();
boolean result = false;
Statement statement = null;
ResultSet resultSet = null;
try {
String sql = "SELECT * FROM input_form WHERE username ='"+username+"'AND password='"+password+"'";
statement=MyCon.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
result=true;
}else {
result = false;
}
} catch (SQLException e) {
}
return result;
}
} ```
And when i need to access that method in servlet, it doesnt see it :
```protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html/charset-UTF-8");
out =response.getWriter();
boolean login = processLogin(username,password); //this is where it doesnt see it
```
what could i do for my servlet to see the method?
创建您需要调用的类的object
,然后使用该object
来调用所需的方法。因此,您的doPost方法将如下所示:
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html/charset-UTF-8");
out =response.getWriter();
Login l1 =new Login();//creating object of class
boolean login = l1.processLogin(username,password); //using object to call the method
//do further process
另外,使用PreparedStatement
选择数据库中的值并避免使用任何SQL injection。因此,您的方法代码如下所示:
PreparedStatement ps = con.prepareStatement("select *from input_form where username=? AND password= ?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
//if true
if (rs.next()) {
result=true;
}else{
result = false;
}