Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to set Persistant Cookies

 
Parthiban Sivaji
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic