在我的 Java servlet 上,我已经初始化了一个 cookie,如下所示,
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
String loc = "/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/"+timeStamp;
Cookie thecookie = new Cookie("thecookie", loc);
thecookie.setMaxAge(60*60*24);
response.addCookie(thecookie);
我在 cookie 评论中添加了一些数据,如下所示,
thecookie.setComment(fileTxt);
现在在我的 jsp 页面上,当我尝试访问此评论时它返回了一个空值,
<%
Cookie my = null;
my.getComment();%>
如何将java中设置的注释值获取到我的jsp页面中??
最佳答案
在你的 JSP 中使用,
<%
Cookie cookie = null;
Cookie[] cookies = null;
cookies = request.getCookies();
if( cookies != null)
{
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
String b = cookie.getComment();
request.setAttribute("xyz", b);
}
}
%>
然后您可以在 html 中使用 is by ${xyz}
,如果您想在 JSP 代码中使用它,则使用 b
。
关于java - 在jsp中获取cookie值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808565/