用户名和密码传过来的不是null,而是一个空的字符串,改成username.equals("")&&password.equals("")
我这里没有运行环境
感觉可能是
String username = request.getParameter("username").toString();
String password = request.getParameter("password").toString();
这里的问题
String username = (String)request.getParameter("username");
String password = (String)request.getParameter("password");
你试试这样写,用强转
你打印一下userName看看到底是个什么东西!
//试试这样行吗!!!关键要细心!!!!!!!
public void doPost
(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
String username = request.getParameter("username").toString();
String password = request.getParameter("password").toString();
if(username!=null&&password!=null) { //如果用户名和密码都不为空
request.setAttribute("unpw", "success"); //设置request参数
if(new UserDAO().testPassword(username,password)){ //如果数据访问测试密码
request.setAttribute("loginResult", "success"); //设置request参数
}
else {//否则
request.setAttribute("loginResult", "failed"); //设置request参数
}
}else{ //否则
request.setAttribute("unpw", "failed"); //设置request参数
}
//这样太费事了!
//RequestDispatcher rd=request.getRequestDispatcher("/loginResult.jsp");
//rd.forward(request,response);
//这样比较简介!
request.getRequestDispatcher("/loginResult.jsp").forward(request,response);//请求转发
//也许是因为你的if--esle的{}问题,最好都加上,即使是只有一句!!!
}