使用JSTL阅读cookie [英] Read cookies with JSTL
问题描述
我正在研究cookie,并且能够使用基于scriptlet的JSP创建和读取它们,但是当我尝试使用JSTL进行同样的操作时,它并不会显示我的所有cookie,仅显示"JSESSIONID ...",而我的其他cookie都不会显示.项目,它显示了我创建的cookie.我的问题是为什么,这似乎对我来说合适,但对我的浏览器来说却不合适. 这是我要使用JSTL进行的操作:
I was studying about cookies and was able to create and read them using scriptlet-based JSP,however when I tried to do the same using JSTL it's not displaying all my cookies, only "JSESSIONID ..." and with my other project it shows the cookie that I created. My question here is why, it seems right for me but not for my browser.. Here is what I'm trying to do using JSTL:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach var="cookieVal" items="${requestScope.cookies}" >
<tr>
<td align="right">${cookieVal.name}</td>
<td>${cookieVal.value}</td>
</tr>
</c:forEach>
我的其他项目正在工作:
And my other project working:
<% Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];%>
<tr>
<td><%=cookie.getName()%></td>
<td><%=cookie.getValue()%></td>
</tr>
<%}%>
我的项目库文件夹中有m个JSTL .jars ... 谢谢!
And I have m JSTL .jars inside my project libraries folder... Thank you!
推荐答案
requestScope.cookies
将搜索名为cookies
的请求属性.如果要访问请求的cookie属性,则需要pageContext.request.cookies
.
requestScope.cookies
will search for an request attribute named cookies
. If you want to access the cookies property of the request, you need pageContext.request.cookies
.
也就是说,访问cookie是您应该在控制器(servlet)内部而不是在视图(JSP)中使用Java代码完成的,访问视图(JSP)仅应处理HTML生成.
That said, accessing cookies is something you should do with Java code, inside a controller (servlet), and not in the view (JSP), which should only deal with HTML generation.
这篇关于使用JSTL阅读cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!