简体   繁体   English

删除.JSP文件中的Java cookie

[英]Deleting Java cookie in .JSP file

I'm using the following function to create a cookie which is set in a Java Servlet. 我正在使用以下函数来创建在Java Servlet中设置的cookie。 I am trying to delete it in a scriptlet in a .JSP file. 我试图在.JSP文件的scriptlet中删除它。 However it is not deleting the cookie, any ideas as to why? 但是它不是删除cookie,关于为什么的任何想法?

This is the function that I am using to create the cookie in the Servlet: 这是我用来在Servlet中创建cookie的函数:

    for(String classId :request.getParameterValues("classId")){
        optionCookie = new Cookie("componentSearchOptionSelect",classId);
        response.addCookie(optionCookie);
    }

This is the code I am using to delete the cookie in the scriptlet: 这是我用来删除scriptlet中的cookie的代码:

Cookie[] cookies = null;
        cookies = request.getCookies();
        if(cookies != null){
            for(int i = 0; i < cookies.length; i++){
                 Cookie cookie = cookies[i];
                 if(cookie.getName().equals("componentSearchOptionSelect")){

                     selectedClass = cookie.getValue();
                     cookie.setMaxAge(0);
                     response.addCookie(cookie);
                 }
             }
        }

JSP as being a view technology is responsible for generating the HTTP response body. JSP作为一种视图技术,负责生成HTTP响应正文。 Cookies have to go in the HTTP response headers. Cookie必须放在HTTP响应标头中。 So if you put that cookie code halfway in a JSP and JSP has at that point already genrated that much of HTML which caused that the response is already committed, then it's simply too late to set a HTTP response header. 因此,如果您将cookie代码放入JSP的中间位置,并且JSP那时已经生成了很多HTML,导致响应已经提交,那么设置HTTP响应标头为时已晚。 The HTTP response headers have already been sent to the client, which is a point of no return. HTTP响应标头已经发送到客户端,这是无法返回的点。 If you have paid attention to the server logs, then you should have noticed an IllegalStateException with a pretty self-explaining message and trace. 如果您已经注意了服务器日志,那么您应该已经注意到了一个IllegalStateException带有一个非常容易解释的消息和跟踪。

So, to fix your problem, just make sure that you delete the cookie when the response isn't committed yet. 因此,要解决您的问题,只需确保在尚未提交响应时删除cookie。 Put the scriptlet containing all the business logic in the very top of the JSP page, long before the JSP writes anything to the HTTP response body. 在JSP向HTTP响应主体写入任何内容之前,将包含所有业务逻辑的scriptlet放在JSP页面的顶部。

<%
    // Business logic here.
%>
<!DOCTYPE html>
<html>
    ... (presentation logic here)
</html>

Actually, JSP is the wrong place to perform business logic (read: you should not be using scriptlets at all ). 实际上,JSP是执行业务逻辑的错误位置(阅读: 您根本不应该使用scriptlet )。 You should be using a servlet or servlet filter for this. 您应该为此使用servletservlet过滤器 In your particular case, I think you just need a servlet with doGet() . 在您的特定情况下,我认为您只需要一个带有doGet()的servlet。

Please try this. 请尝试这个。

/*
Cookie[] cookies = null;
cookies = request.getCookies();
if(cookies != null){
    for(int i = 0; i < cookies.length; i++){
         Cookie cookie = cookies[i];
         if(cookie.getName().equals("componentSearchOptionSelect")){
             selectedClass = cookie.getValue();
             cookie.setMaxAge(0);
             response.addCookie(cookie);
         }
     }
}
*/

Cookie cookie = new Cookie("componentSearchOptionSelect", "");
cookie.setMaxAge(0);
response.addCookie(cookie);

By the way, Why do you set same value to Cookie many times? 顺便说一句,为什么您多次设置相同的Cookie值? The value of the cookie componentSearchOptionSelect is the value of the last from request.getParameterValues("classId")) . cookie componentSearchOptionSelect的值是request.getParameterValues("classId"))最后一个的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM