How to use cookies to enable cross-context communication.
To make the cookies available to other apps you need to set setPath() method to the root path by using cooki.setPath("/");
Servlet Code:
Cookie cooki = new Cookie("session","visu");
System.out.print("Cookie setting for first time");
cooki.setPath("/");
cooki.setMaxAge(10); //48*60*60
response.addCookie(cooki);
response.sendRedirect("/SessionExample/MyJsp.jsp");
JSP Code:
<%
int i = 0;
while( cookieArr!=null && i <>
Cookie cookie = cookieArr[i];
System.out.println( cookie.getName()+" = " + cookie.getValue() );
out.print(cookie.getName()+" = " + cookie.getValue() );
i++;
}
%>
You can tell a cookie to stay alive even AFTER the browser restarted (or) the user hasn’t been on the site for a week. This can be achieved using cooki.setMaxAge(10); (//48*60*60) method.
0 Comments:
Post a Comment
<< Home