如何在JSTL中设置cookie [英] How to set a cookie in JSTL

查看:319
本文介绍了如何在JSTL中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现以下但不确定其可能和语法应该是什么?

I am trying to achieve the below but not sure if its possible and what should the syntax be ?

<c:if condition is true>
  <c:set cookie using JSTL>
</c:if>

我想,因为cookie只在客户端设置,JSTL用于在服务器方面,它不能做。

I am thinking ,since cookie is only set on client side and JSTL is used for doing things on server side, it cannot be done.

推荐答案

您不能使用JSTL设置cookie。 JSTL 没有此功能的任何标签。 JSTL在生成HTTP响应期间运行,而在生成HTTP响应之前,需要在HTTP响应头中设置一个Cookie。

You can't set a cookie using JSTL. JSTL doesn't have any tags for this functionality. JSTL runs during generating the HTTP response, while a cookie needs to be set in the HTTP response header long before generating the HTTP response. There's otherwise means of an illegal state.

使用预处理 servlet 过滤器,其中您只需调用 response.addCookie()

Use a preprocessing servlet or filter instead wherein you can just call response.addCookie()

这篇关于如何在JSTL中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆