Source Code : Cookie Demo
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Cookie Demo
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
Cookie cookie = null;
Cookie[] cookies = request.getCookies();
boolean newCookie = false;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("mycookie")) {
cookie = cookies[i];
}
}
}
if (cookie == null) {
newCookie = true;
int maxAge;
try {
maxAge = new Integer(getServletContext().getInitParameter(
"cookie-age")).intValue();
} catch (Exception e) {
maxAge = -1;
}
cookie = new Cookie("mycookie", "" + getNextCookieValue());
cookie.setPath(request.getContextPath());
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Cookie info</title>");
out.println("</head>");
out.println("<body>");
out
.println("<h2> Information about the cookie named "mycookie"</h2>");
out.println("Cookie value: " + cookie.getValue() + "<br>");
if (newCookie) {
out.println("Cookie Max-Age: " + cookie.getMaxAge() + "<br>");
out.println("Cookie Path: " + cookie.getPath() + "<br>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
private long getNextCookieValue() {
return new java.util.Date().getTime();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request, response);
}
}
Thank with us