日期:2014-05-16  浏览次数:20721 次

用ajax写用户名验证时,在servlet写了个跳转语句不能执行

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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?ajax?jsp

------解决方案--------------------
再客户端跳转,服务端跳转ajax不会理会的

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


if(ajaxobj.responseText=='1')window.location='login.jsp'