• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cookies on JSP

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I have have read that you are not allowed to write cookies in a JSP and JSTL does not allow you to write cookies but it does allow you to read them.
Now suppose a user goes directly to a page say "page1.jsp" now page 1 will always redirect to another page.

My question is how can i set a particular cookie at the address of page1.jsp (since the container is calling Page1.jsp directly) so that when ever a user gets to page1.jsp the browser sends the cookie of page1.jsp to it.
The only way i see this to be possible is to create a servlet which sets a cookie and forwards it to page1.jsp. something like this
Container->Servlet->(forward with cookie)->page1.jsp
But i need something like this
Container->page1.jsp(i need to place a cookie here)

a scriptlet could do the task at hand easily like this
<%
Cookie cookie = new Cookie("name","John");
response.addCookie(cookie);
%>
but i dont want to use a scriptlet. What would be the best way to accomplish this task ?? any suggestions ??

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just do it in the page controller servlet for the JSP.

If you don't know what that is, please read this article.
 
sagar kumar nerella
Greenhorn
Posts: 18
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can use the jsp standard tag libraries .
i am not sure of this tag <jsp:cookie> but there is a way to set cookie from a jsp with out using scriplets.
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no standard action named <jsp:cookie>. And there is no other standard action which deals with cookies. You can get cookie values using EL. But there is no way you can set them in JSP (without using scriptlet). This doesn't mean that go ahead and use scriptlets for setting cookies. Use page controller servlet for the JSP as mentioned by Bear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!