简体   繁体   中英

How to set Cookies in Java?

I am writing a program to login to my University web site as a part of my project. The problem I have is how to set Cookies and let my browser enable these cookies, my code is below and gives me NullPoiterException, what I did wrong can someone help me:

    for (String cookie : this.cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
    }

Part of My servlet, how i set cookie.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String ksywa = request.getParameter("ksywa");
    out.print("Twoje nazwisko to " + ksywa);
    Cookie cookie = new Cookie("pseudonim", ksywa);
    cookie.setMaxAge(60*60);
    response.addCookie(cookie);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM