user7723264
user7723264

Reputation: 21

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

Upvotes: 0

Views: 402

Answers (1)

xcesco
xcesco

Reputation: 4838

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.

Upvotes: 1

Related Questions