大侠来帮我看看程序,关于servlet的小程序
if判断用户名和密码的时候为什么每次都是执行response.sendRedirect("/WebDL/PasswordError.html");
这一句?这是密码错误的时候才执行的,是不是哪里写错了?小弟新手刚学j2ee求大侠指点
public class DLServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String password1 = request.getParameter("password");
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest";
String user = "sa";
String password= "aaaaaa";
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM [USER] WHERE NAME =?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
//st.executeUpdate(sql1);
ResultSet rs =ps.executeQuery();
String doPasswd;
if(rs.next()){
doPasswd = rs.getString("passwd");
if(doPasswd.equals(password1)){
response.sendRedirect("/WebDL/Welcome.html");
}else{
response.sendRedirect("/WebDL/PasswordError.html");
}
}else{
response.sendRedirect("/WebDL/Error.html");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
[解决办法]
楼主断点过
if(doPasswd.equals(password1)) 这一行是true or false了吗?两个密码值相等吗?
[解决办法]
用list实现查询数据库信息并打印出来是可以的,或者写一个方法,返回结果并打印。但是我觉得你写的界面有问题,为什么不用jsp呢?再好好看看你的界面代码吧,还有数据库连接最好封装一下,不然重复代码太多,看着也乱