简体   繁体   中英

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 [] 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. How would I do that..

${vav}

jsp file.. Thank you all in advance..

To display cookie value into a JSP, use this code:

<%@ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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