Cookie Basics--Simple test

Source: Internet
Author: User
Tags set cookie

================ the servlet class serveletsetcookies ====================== setting cookies

Import java.io.IOException; 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 Serveletsetcookies extends httpservlet{


@Override
protected void doget (Httpserv Letrequest req, HttpServletResponse resp)
throws Servletexception, IOException {
System.out.println ("Set Cookies" );
Cookie ci=new Cookie ("password", "123");
//Ci.setmaxage (2 * 60 * 60 * 1000);//set to 2 clock
Cookie Ci2=new cookie ("Password2", "123456");
//ci.setmaxage (20 * 60 * 60 * 1000);//set to 2 clock
Resp.addcookie (CI);
Resp.addcookie (CI2);
Resp.getwriter (). println ("Set cookie OK. ");
}

@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
throws Servle Texception, IOException {
This.doget (req, resp);

}

}

=============== the servlet class serveletshowcookies ============ setting cookies

Import java.io.IOException;
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 Serveletshowcookies extends httpservlet{

@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
////////////////
System.out.println ("Show cookies");
Cookie[] Cookies=req.getcookies ();
for (int i=0;i<cookies.length;i++) {
Cookie C=cookies[i];
Resp.getwriter (). println (C.getname () + "," +c.getvalue ());
}
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
This.doget (req, resp);
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.