aspose file tools*
The moose likes Servlets and the fly likes how to set Persistant Cookies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to set Persistant Cookies" Watch "how to set Persistant Cookies" New topic
Author

how to set Persistant Cookies

Parthiban Sivaji
Greenhorn

Joined: Apr 27, 2000
Posts: 5
Hi,
How can I set a persistant cookie. I tried using Cookie class setMaxAge method , this is not creating a persistant cookie, when the browser exits this cookie is deleted from the memory.I want to store a cookie file in the client machine. How can I do it ??
Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
I think cookies automatically contain persistance. Looking back at my notes from class I noticed a statement that mentions that a cookie is sent back as part of the header. You need to make sure you send the cookie (res.addCookie(...)) before you send the body of the HTML. Also It may be a better choice to use the HTTPSession object because you don't need to deal with cookies directly. Hope this helps.
Parthiban Sivaji
Greenhorn

Joined: Apr 27, 2000
Posts: 5
Hi Joe,
Usage of session object is fine,but I need to see the cookie.txt file in the hard disk. res.addCookie just has the cookie in the memory for that session, if session is closed, cookie is also erased.
Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
I put together a sample Servlet and it seemed to work. My browser is IE 5.0 which has a Cookies folder and an Index.dat file (which I think replaces Cookie.txt). I saved the Cookie, exited the browser, started the browser, and displayed the cookie. If this program does not work for you then you may have a problem with your browser or browser options.
JAVA source:
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestServlet extends HttpServlet {

public void init(ServletConfig sc) throws ServletException {
super.init(sc);
}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
try {
res.setContentType("text/html");
out.println("<html><head><title>Test Servlet</title>");
String action = req.getParameter("action");
if ("Show Cookie".equalsIgnoreCase(action)) {
doShowCookie(req,res);
}else if("Save Cookie".equalsIgnoreCase(action)) {
doSaveCookie(res);
}else {
out.println("Invalid request:"+ action);
}
}catch (Exception e) {
e.printStackTrace(out);
} finally {
out.println("</body></html>");
}
}

private void doShowCookie(HttpServletRequest req, HttpServletResponse res) {

try {
PrintWriter out = res.getWriter();
out.println("</head><body><h2>Cookie List</h2>");
Cookie[] ckIn = req.getCookies();
for (int i= 0; i < ckIn.length; i++) {
out.println("Cookie Name = " + ckIn[i].getName()
+ "Cookie Value = " + ckIn[i].getValue());
}
} catch (Exception e) {
System.out.println("Error = " + e);
}

}
private void doSaveCookie(HttpServletResponse res) {

try {
PrintWriter out = res.getWriter();
Cookie ckOut = new Cookie("UserId","JavaUser");
ckOut.setMaxAge(10000000);
res.addCookie(ckOut);
out.println("</head><body><p>Cookie Saved!</p>");

} catch (Exception e) {
System.out.println("Error = " + e);
}
}
}
HTML Source:
<html>
<head>
<meta http-equiv=Pragma content=no-cache>
<title>Test Servlet</title>
</head>
<h2>Test Servlet</h2>
<br><br><br><br>
<body bgColor=#CCCC99>
<p>
<form action=servlet/TestServlet method=get>
<table border="0">
<tr><td>Save Cookie<input type=submit name=action value="Save Cookie"</td></tr>
<tr><td>Show Cookie<input type=submit name=action value="Show Cookie"</td></tr>
</table>
</form>
</p>
</body>
</html>
Parthiban Sivaji
Greenhorn

Joined: Apr 27, 2000
Posts: 5
Hi Joe,
Thanx for that reply. It is working fine for me now. I had made a silly mistake in my program. I had set the age of the Cookie after adding cookie.
Thanx again,
Parthiban
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to set Persistant Cookies