【发布时间】:2014-03-05 22:49:34
【问题描述】:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); //setting the MIME of this page
PrintWriter out = response.getWriter(); //creating a writer object
String sourceCookieName = "Username";
String sourceCookieName2 = "Password";
Cookie targetCookie = null;
Cookie targetCookie2 = null;
Cookie[] allCookies = request.getCookies();
if (allCookies!=null) {
for (Cookie cookie : allCookies) {
if (cookie.getName().equals(sourceCookieName)) {
targetCookie = cookie;
//break;
}
if (cookie.getName().equals(sourceCookieName2)) {
targetCookie2 = cookie;
//break;
}
}
}
if (targetCookie != null && targetCookie2 != null) {
//somecode
}
else {
out.print("<p><h1>You will be redirected in <span id='counter'>5</span> second(s).</h1></p>");
out.print("<script type='text/javascript'>");
out.print("function countdown() {");
out.print("var i = document.getElementById('counter');");
out.print("if (parseInt(i.innerHTML)<=1) {");
out.print("location.href = 'index.html';");
out.print("}");
out.print("i.innerHTML = parseInt(i.innerHTML)-1;");
out.print("}");
out.print("setInterval(function(){ countdown(); },1000);");
out.print("</script>");
//response.sendRedirect("index.html");
return;
}
我有这个 servlet,它会在 cookie 被删除时重定向到 index.html。我的问题是,当我删除 cookie 时,它不会自动重定向到 index.html 我需要在重定向之前先重新加载/刷新页面。删除 cookie 后如何使其自动重定向?
【问题讨论】:
标签: java servlets redirect cookies