我有一個名爲apt1.java的用戶定義包,我將它導入到一個名爲aptservlet1.java的servlet中。它正確運行,但只返回錯誤的值,無關正確的論點。如果我通過簡單的java程序(在命令提示符下有輸出)使用此包,而不是提供正確答案。java包將值返回給servlet
請回答這個問題.. fllowing是我的節目..
aptservlet1.java:
import java.io.IOException;
import java.io.PrintWriter;
public class aptservlet1 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
boolean correct;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String user, pass;
user = req.getParameter("user");
pass = req.getParameter("pass");
apt1 o1 = new apt1();
correct = o1.confirmadmin(user, pass);
out.println("value of correct=" + correct);
ServletContext sc = this.getServletContext();
if (correct) {
out.println("correct");
}
if (correct) {
out.println("incorret");
}
}
}
apt1.java:
public class apt1 {
String url = "jdbc:mysql://localhost/" + "aptitude";
public apt1() {
try {
Class.forName("con.mysql.jdbc.Driver");
} catch (ClassNotFoundException ce) {
}
}
public boolean confirmadmin(String user, String pass) {
boolean c = false;
try {
String url = "jdbc:mysql://localhost/" + "aptitude";
Connection con = DriverManager.getConnection(url, "root", "");
Statement s = con.createStatement();
ResultSet rs = s
.executeQuery("select a_pass from admin_database where a_user='"
+ user + "'");
rs.next();
String s1 = rs.getString(1);
int k = s1.compareTo(pass);
if (k == 0) {
c = true;
} else {
c = false;
}
} catch (SQLException se) {
}
return c;
}
}
你會考慮更換'INT K = s1.compareTo(合格),'和下面如果'返回s1.equals/else區塊(合格),'。 – Tom 2014-09-03 20:32:06
反正你做錯了。您應該搜索匹配該用戶*和密碼的行,並僅測試是否有一行。讓數據庫做比較。密碼也應該被散列,這也是數據庫可以爲你做的。 – EJP 2014-09-03 21:41:48