简单实例:
- packagecom.nothing2012.servlet.cookie;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjavax.servlet.RequestDispatcher;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.Cookie;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- /**
- *ServletimplementationclassSaveCookie
- */
- publicclassSaveCookieextendsHttpServlet{
- privatestaticfinallongserialVersionUID=1L;
- /**
- *@seeHttpServlet#HttpServlet()
- */
- publicSaveCookie(){
- super();
- //TODOAuto-generatedconstructorstub
- }
- /**
- *@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
- */
- protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- //TODOAuto-generatedmethodstub
- doPost(request,response);
- }
- /**
- *@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
- */
- @SuppressWarnings("unused")
- protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- //TODOAuto-generatedmethodstub
- response.setContentType("text/html;charset=UTF-8");
- PrintWriterout=response.getWriter();
- CookietempCookie=newCookie("temp","87654321");
- tempCookie.setMaxAge(-1);
- response.addCookie(tempCookie);
- Cookiecookie=newCookie("cookie","666");
- cookie.setMaxAge(0);
- response.addCookie(cookie);
- Stringuser=request.getParameter("user");
- if(user!=null){
- CookieuserCookie=newCookie("user",user);
- userCookie.setMaxAge(60*1*1);
- response.addCookie(userCookie);
- }
- System.out.println("=========writecookieend=========");
- RequestDispatcherreadCookie=getServletContext().getRequestDispatcher("/readCookie");
- readCookie.include(request,response);
- }
- }
- packagecom.nothing2012.servlet.cookie;
- importjava.io.ByteArrayInputStream;
- importjava.io.IOException;
- importjava.io.ObjectInputStream;
- importjava.io.PrintWriter;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.Cookie;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- /**
- *ServletimplementationclassreadCookie
- */
- publicclassReadCookieextendsHttpServlet{
- privatestaticfinallongserialVersionUID=1L;
- /**
- *@seeHttpServlet#HttpServlet()
- */
- publicReadCookie(){
- super();
- //TODOAuto-generatedconstructorstub
- }
- protectedCookiegetCookie(Cookie[]cookies,Stringname){
- if(cookies!=null){
- for(Cookiec:cookies){
- if(c.getName().equals(name)){
- returnc;
- }
- }
- }
- returnnull;
- }
- /**
- *@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
- */
- protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- //TODOAuto-generatedmethodstub
- doPost(request,response);
- }
- /**
- *@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
- */
- protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- //TODOAuto-generatedmethodstub
- response.setContentType("text/html;charset=UTF-8");
- PrintWriterout=response.getWriter();
- System.out.println("========beginreadcookie===========");
- CookietempCookie=getCookie(request.getCookies(),"temp");
- if(tempCookie!=null){
- out.println("临时cookie值:"+tempCookie.getValue()+"<br/>");
- }else
- out.println("cookie已经被删除<br/>");
- Cookiecookie=getCookie(request.getCookies(),"cookie");
- if(cookie!=null){
- out.println("cookie:"+cookie.getValue()+"<br>");
- }else
- out.println("这个cookie永远不可能获得<br>");
- CookieuserC=getCookie(request.getCookies(),"user");
- if(userC!=null){
- out.println("user="+userC.getValue());
- }else
- out.println("user未创建");
- }
- }
package com.nothing2012.servlet.cookie;
@SuppressWarnings("serial")
public class MyCookie implements java.io.Serializable{
public String getMsg(){
return "222";
}
}
读取复杂对象:
- publicclassSaveComplexCookieextendsHttpServlet
- {
- @Override
- protectedvoidservice(HttpServletRequestrequest,
- HttpServletResponseresponse)throwsServletException,IOException
- {
- //创建一个用于进行Base64编码的BASE64Encoder对象
- sun.misc.BASE64Encoderbase64Encoder=newsun.misc.BASE64Encoder();
- //创建一个用于接收被序列化的对象实例字节流的ByteArrayOutputStream对象
- ByteArrayOutputStreamclassBytes=newByteArrayOutputStream();
- //创建一个用于向流中写入对象的ObjectOutputStream对象
- ObjectOutputStreamoos=newObjectOutputStream(classBytes);
- oos.writeObject(newMyCookie());//写入MyCookie对象实例
- oos.close();//关闭ObjectOutputStream对象
- //将被序列化的对象实例的字节流按Base64编码格式进行编码
- StringclassStr=base64Encoder.encode(classBytes.toByteArray());
- Cookiecookie=newCookie("mycookie",classStr);//将Base64编码写入Cookie
- cookie.setMaxAge(60*60*24);//Cookie的超时时间为1天
- response.addCookie(cookie);
- response.setContentType("text/html;charset=utf-8");
- PrintWriterout=response.getWriter();
- out.println("MyCookie的对象实例已写入Cookie");//输出提示消息
- }
- }
- publicclassReadComplexCookieextendsReadCookie
- {
- @Override
- publicvoidservice(HttpServletRequestrequest,
- HttpServletResponseresponse)throwsServletException,IOException
- {
- try
- {
- sun.misc.BASE64Decoderbase64Decoder=newsun.misc.BASE64Decoder();
- Cookiecookie=getCookie(request.getCookies(),"mycookie");
- if(cookie==null){
- System.out.println("----------------");
- return;
- }
- System.out.println("cookie更新不");
- StringclassStr=cookie.getValue();
- System.out.println(classStr);
- byte[]classBytes=base64Decoder.decodeBuffer(classStr);
- ObjectInputStreamois=newObjectInputStream(
- newByteArrayInputStream(classBytes));
- MyCookiemyCookie=(MyCookie)ois.readObject();
- response.setContentType("text/html;charset=utf-8");
- PrintWriterout=response.getWriter();
- out.println(myCookie.getMsg());
- System.out.println("========"+myCookie.getMsg());
- }
- catch(Exceptione)
- {
- e.printStackTrace();
- }
- }
- }
后者测试未通过:报java.io.EOFException