• Post Reply Bookmark Topic Watch Topic
  • New Topic

cookie problem

 
somayeh khodadad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
i want to know how can i use a cookie of an application in another application ?

for example,I have an jsp page at (http://localhost:8080/applicationA/setcookie.jsp ) that creates a new cookie and sets name and value for it. Then i want to use that cookie in another page in applicationB at (http://localhost:8080/applicationB/usecookie.jsp) .

is it correct to use (cookie.setDomain("/localhost") after creating new cookie ???

thanks in advance.
Somayeh.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somayeh,
Welcome to CodeRanch!

Yes. As long as you set the cookie at the level of the common ancestor, both applications can use it.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed you are using the same domain for both already. You also have to call cookie.setPath("/") so it gets set at the logical root level rather than "/ApplicationA".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please click this link ⇒ CarefullyChooseOneForum ⇐ for more information.

I have removed the duplicate post.
 
somayeh khodadad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks for your answer dear Jeanne.
i've used both of cookie.setPath("/") and cookie.setDomain("localhost") in my code before. but i can't see that cookie in my applicationB :(

I attach my code with this post , I would be thankfull if you check it for me.
Somayeh.
------------------------------------------------------------------
<!-- http://localhost:8080/applicationA/setcookie.jsp -->

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

<%
Cookie cookie = new Cookie ("cookieName","cookieValue");
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);

cookie.setPath("/");
cookie.setDomain("localhost");
%>

<html>
<head>
<title>Cookie Saved</title>
</head>
<h1>Cookie created and saved</h1>

</html>

---------------------------------------

<!-- http://localhost:8080/applicationB/showcookie.jsp -->

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

<html>
<head>
<title>Show Saved Cookie</title>
</head>
<h1>Show cookies</h1>
<body>
<p>
List of Cookies:<br/>
<c:forEach items="<%=(request.getCookies())%>" var="c">
  
<b><c:out value="${c.name}" /></b>:
<c:out value="${c.value}" /><br>
</c:forEach>
</body>
</html>

 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you certain you set the cookie right?
http://download.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html#setDomain%28java.lang.String%29

You set the cookie in application A but can you access the cookie in application A after you set it? If you cannot access it even there then you set the cookie the wrong way.
 
somayeh khodadad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi dear Ilari,
yes, i can access to cookie in applicationA after setting the cookie, but not in applicationB .
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Then it might be the part where you set the domain.

Have you read about the "localhost" problem? Google finds many pages of it including
http://forums.sun.com/thread.jspa?threadID=704928
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree the problem is likely using "localhost."

Also note it is bad practice to call Java code from a JSP. It would be better to set the Cookie in the servlet code before forwarding to the JSP.
 
somayeh khodadad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks for your advice i'll try servlet and tell you the result .
 
somayeh khodadad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi dear Jeanne ,
thanks for your advice. my problem solved.
i used servlet and put cookie.setPath("/") in my servlet. then every application have access to cookies which were created in servlet



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somayeh,
Great! Thanks for coming back to say the problem is solved. It provides nice closure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!