How to Access Cookies in JSP Expression Language

Below snippet is just for your reference. We can print a cookie value on JSP page using JSP Expression language.


The standard syntax to access Http Cookie value in JSP is:



${cookie.<cookie name>.value}

So if you want to print value of cookie named “foo” on JSP page, you might wanna write something like:



${cookie.foo.value}

In previous tutorial How to access Cookies in Spring MVC, I used JSP expression language to print value of hitCounter cookie.


Display List of All Cookies in JSP


In JSP expression language ${cookie} gives a list of all cookies set for current webpage. This list can be iterated using JSTL <c:forEach> to print each cookie. Here is a small JSTL code snippet that prints list of all cookies for current page.



<h3>List of all the available Cookies</h3>
<ul>
<c:forEach var="cookies" items="${cookie}">
<li>
<c:out value="${cookies.key}"/>:
Object=<c:out value="${cookies.value}"/>,
value=<c:out value="${cookies.value.value}"/>
</li>
</c:forEach>
</ul>

Once you run this code, you’ll see list of all http cookies for given page as below.

http-cookies-jsp-expression-language


Hope you’ll remember this little trick next time you want something like this.








via ViralPatel.net http://feedproxy.google.com/~r/viralpatelnet/~3/1JJPi10ZwvE/

No comments:

Post a Comment

If you have any question please let me know

Windows 11 Insider Preview Build 29558.1000 Released to New Canary Channel

UPDATE: Windows 11 Insider Preview build 29558.1000 released to the new Canary channel. Windows Insiders on the refreshed Canary Channel wil...