The following is an example code for using cookies in jsp.
- <% @ Page contentType = "text/html" pageEncoding = "GBK" %>
<% @ Page import = "javax. servlet. http. Cookie, java. util. *" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
<Title> JSP Page </title>
</Head>
<Body>
<%
// String CookiesName = "username ";
// Cookie cookie_username = new Cookie ("username", CookiesName );
// Response. addCookie (cookie_username );
Int click = 0;
Cookie [] cookies = request. getCookies ();
Cookie cookie_reponse = null;
List list = Arrays. asList (cookies );
Iterator it = list. iterator ();
While (it. hasNext ()){
Cookie temp = (Cookie) it. next ();
If (temp. getName (). equals ("clicktimes ")){
Click = Integer. parseInt (temp. getValue ());
Cookie_reponse = temp;
Break;
}
}
// Obtain the click Value
// Output
Out. println ("click" refresh ");
// Update
Clickclick = click 1;
If (cookie_reponse = null ){
// Empty
Cookie_reponse = new Cookie ("clicktimes", String. valueOf (click ));
} Else {
Cookie_reponse.setValue (String. valueOf (click ));
}
Response. addCookie (cookie_reponse );
Response. setContentType ("text/html ");
Response. flushBuffer ();
%>
</Body>
</Html>