日期:2014-05-17  浏览次数:20821 次

在servlet写了个跳转语句不能执行

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletExceptionIOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
UserDao ud=new UserDao();
String username=request.getParameter("user");
String userpassword=request.getParameter("pwd");
boolean flag=ud.sel(username);
System.out.println(username);
System.out.println(userpassword);
System.out.println(flag);
PrintWriter out = response.getWriter();
if(flag){
out.print("The user name has been registered!");
}else{
out.print("<script>window.location.href='login.jsp';</script>");
//response.sendRedirect("login.jsp");
}
}

用out.print("<script>window.location.href='login.jsp';</script>");显示

用response.sendRedirect("login.jsp");这个显示
servlet?jsp?ajax

------解决方案--------------------
试试这个:

if(flag){
            out.print("The user name has been registered!");
        }else{
            //out.print("<script>window.location.href='login.jsp';</script>");
            response.sendRedirect("login.jsp");
        }