如何在JSF中使用cookie

如何在JSF中使用cookie

本文介绍了如何在JSF中使用cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个JSF形式的JSF 1.2会话范围Bean。 我有一个重置按钮,使会话无效。I have a JSF form over a JSF 1.2 Session Scope Bean.I have a "Reset" button which invalidates the session.我试图使用cookies记住会话号(非JSF会话,但我的私人会话号)之间,但我失败。 我的问题 - 正确的地方(一些监听器?Bean构造函数?)初始化,检索和存储cookie。I tried to use cookies to remember a session number (Not JSF session but my private session number) between sessions but I failed.My question - Where is the correct place (Some listener? Bean Constructor?) to initialize, retrieve and store the cookie.寻找最好的方法谢谢!推荐答案由 ExternalContext#getRequestCookieMap()Map<String, Object> cookies = externalContext.getRequestCookieMap();// ... 当在Servlet API上运行JSF时在99.9%的情况下);),映射值解析为 javax.servlet.http.Cookie 。Cookie cookie = (Cookie) cookies.get(name);// ... 在JSF 1.2中,缺少JSF 2.0引入的 ExternalContext#addResponseCookie() 方法,您需要投射 ExternalContext#getResponse() 到 HttpServletResponse (仅当运行JSF在Servlet API之上),然后使用 HttpServletResponse#addCookie() 。HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();Cookie cookie = new Cookie(name, value);cookie.setMaxAge(maxAge); // Expire time. -1 = by end of current session, 0 = immediately expire it, otherwise just the lifetime in seconds.response.addCookie(cookie);您可以在JSF上下文中的任何位置执行此操作,正确的位置取决于唯一的功能要求。您只需要确保在响应已提交时不添加Cookie,否则会导致 IllegalStateException 。You can do this anywhere in the JSF context you want, the right place depends on the sole functional requirement. You only need to ensure that you don't add the cookie when the response has already been committed, it would otherwise result in an IllegalStateException. 这篇关于如何在JSF中使用cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 00:21