简体   繁体   English

如何从Java到JSP检索Cookie值

[英]How to retrieve cookie values from java to jsp

String userNmae = req.getParameter("userName");
Cookie ck = new Cookie("hello", vall);
res.addCookie(ck);

//lets suppose, I've stored 5 different users name or integer in cookie. //让我们假设,我在cookie中存储了5个不同的用户名或整数。

Cookie [] cookies = req.getCookies();
            String name;
        for(Cookie cookie : cookies) {
                 name = cookie.getValue();
                req.setAttribute("vav", name);
                req.getRequestDispatcher("index.jsp").forward(req, res);
              }

//Now I would like to retrieve all the values and display on jsp page. //现在,我想检索所有值并显示在jsp页面上。 How would I do that.. 我该怎么办..

${vav}

jsp file.. Thank you all in advance.. jsp文件..谢谢大家..

To display cookie value into a JSP, use this code: 要将cookie值显示到JSP中,请使用以下代码:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach var="cookieVal" items="${pageContext.request.cookies}" > 
    <tr>
        <td align="right">${cookieVal.name}</td>
        <td>${cookieVal.value}</td>
    </tr>
</c:forEach>

This piece of code was originally taken from this answer . 这段代码最初是从此答案中获取的

I hope this helps. 我希望这有帮助。

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

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