McDuck4
McDuck4

Reputation: 662

Cookie gives an error with date

I followed a tutorial on youtube regarding creating a cookie. I typed in the correct code, but I get an error in the line with:

cookie = new Cookie("test_cookie", Long.toString(new Date().getTime()));

The error says: The constructor Date() is undefined. Does anyone know how I get this error, when he can run it in the video: https://www.youtube.com/watch?v=hOColvr3pl0

I guess there should be a parameter in it, or? Best Regards Mads

package ExamplePackage;

import java.io.IOException;
import java.util.Date;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/CookieUsageServlet")
public class CookieUsageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = getCookie(request, response);
        printResponse(response, cookie);
    }

    private Cookie getCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = null;
        Cookie[] cookiesArray = request.getCookies();
        if(cookiesArray != null) {
            for(int x = 0; x < cookiesArray.length; x++) {
                if(cookiesArray[x].getName().equals("test_cookie")) cookie = cookiesArray[x];
            }
        }
        if(cookie != null) {
            System.out.println("cookie retrieved");
        } else {
            int timeToExpireCookie = new Integer(getServletContext().getInitParameter("time-to-expire-cookie")).intValue();
            cookie = new Cookie("test_cookie", Long.toString(new Date().getTime()));
            cookie.setMaxAge(timeToExpireCookie);
            cookie.setPath(request.getContextPath()); // Sti til login
            System.out.println("Cookie created...");
            response.addCookie(cookie);
        }
        return cookie;
    }
    private void printResponse(HttpServletResponse response, Cookie cookie) throws IOException {
        PrintWriter printWriter = response.getWriter();
        response.setContentType("text/html");
        printWriter.println("<html>");
        printWriter.println("<head><title>Cookie In JAVA servlet</title></head>");
        printWriter.println("<body>");
        printWriter.println("<cookie name: " + cookie.getName() + "<br>");
        printWriter.println("<cookie value: " + cookie.getValue() + "<br>");
        printWriter.println("<time to expire cookie in seconds: " + cookie.getMaxAge() + "<br>");
        printWriter.println("</body>");
        printWriter.println("</html>");


    }

}

Upvotes: 1

Views: 286

Answers (2)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41230

java.sql.Date does not have no-arg constuctor.

java.sql.Date change it to java.util.Date

cookie = new Cookie("test_cookie", Long.toString(new java.util.Date().getTime()));

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503459

You're importing java.sql.Date - which doesn't have a parameterless constructor. You meant java.util.Date, which does.

However, there's no need to create a Date object at all in order to get the current time in milliseconds-since-the-Unix-epoch:

cookie = new Cookie("test_cookie", Long.toString(System.currentTimeMillis());

(Or ideally, inject some kind of Clock representation into your code, so you can test it more easily.)

Upvotes: 2

Related Questions