在写sevlet的dopost方法时判定成功的跳转语句总是不好使
我写的是一个登陆程序,从数据库中判定用户名和密码,成功了跳转到主页,否则还是回到登陆页。在写servlet的dopost方法时,用了个判断是否与数据库中的username和password相同,相同就跳转到主页,否则重定向到登陆页。
现在否则成功了,但是在成功的时候代码不报错,一运行就报错。
下面是我的源代码:
package com.servlet;
import
import
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.UserDao;
public class LoginServlet extends HttpServlet {
UserDao userdao = new UserDao();
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + "\t" + password);
try {
boolean bool = userdao.login(username, password);
if (bool == true) {
request.getRequestDispatcher("/jsp/index.jsp").forward(request,
response); }
//问题就出在这,如果用个控制台输出的语句可以运行出来,但是一加上这句话就出问题
else {
response.sendRedirect("/test/jsp/login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}