import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ChangeColorAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws IOException, Exception
{
response.setCharacterEncoding("UTF-8");
HttpSession sess = request.getSession(true);
PrintWriter out=null;
try{
out= response.getWriter();
} catch(Exception e){e.printStackTrace();}
String color = request.getParameter("color");
//读入cookie
Cookie cookies[]=request.getCookies(); // 将适用目录下所有Cookie读入并存入cookies数组中
Cookie sCookie=null;
String sname=null;
String name=null;
if(cookies==null) {// 如果没有任何cookie
System.out.println("none any cookie");
name="";
}
else
{
System.out.println(cookies.length + "<br>");
for(int i=0;i<cookies.length; i++) // 循环列出所有可用的Cookie
{
sCookie=cookies[i];
sname=sCookie.getName();
if("OAcolor".equals(sname))
name = sCookie.getValue();
System.out.println(sname + "->" + name + "<br>"+sCookie.getPath());
}
}
//写入cookie
Cookie _cookie=new Cookie("OAcolor", color);
_cookie.setMaxAge(60*60*24*30*12); // 设置Cookie的存活时间为30分钟
_cookie.setPath("/");
response.addCookie(_cookie); // 写入客户端硬盘
System.out.println("写Cookie完成");
sess.setAttribute("color", name);
return null;
}
}