问题描述
importjava.io.IOException;//为什么下次打开游览器时记不住上次的设置呢//比如我上次选择的是红色字体下次打开的时候没有记住importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassColorCookieextendsHttpServlet{publicColorCookie(){super();}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriter();Cookiecookie=null;Cookiebgcookie=null;Cookie[]cookies=request.getCookies();//Stringcolor=newString("Color");Stringbgcolor=newString("bgColor");Stringcolorvalue=request.getParameter("radio");Stringbgcolorvalue=request.getParameter("radio1");out.println("<HTML>");out.println("<HEAD><TITLE>CookieReader</TITLE></HEAD>");if(cookies!=null&&(colorvalue==null||bgcolorvalue==null)){for(inti=0;i<cookies.length;i++){cookie=cookies[i];color=cookie.getName();if(color.equals("Color")){colorvalue=cookie.getValue();cookie=newCookie(color,colorvalue);cookie.setMaxAge(60*60);}response.addCookie(cookie);}for(inti=0;i<cookies.length;i++){bgcookie=cookies[i];bgcolor=bgcookie.getName();if(color.equals("bgColor")){bgcolorvalue=cookie.getValue();bgcookie=newCookie(bgcolor,bgcolorvalue);bgcookie.setMaxAge(60*60);}response.addCookie(bgcookie);}}out.println("<BODYbgcolor=""+bgcolorvalue+""><h2>");out.println("<fontcolor=""+colorvalue+"">");out.println("<formmethod="get"name="form1">");out.println("请选择你喜欢的字体颜色");out.println("<center><tableborder="1"align="center">");out.println("<tbody><tr><tdbgcolor="red"><inputtype="radio"name="radio"value="red"><br></td>");out.println("<tdbgcolor="yellow"><inputtype="radio"value="yellow"name="radio"><br></td>");out.println("<tdbgcolor="green"><inputtype="radio"value="green"name="radio"><br></td>");out.println("<tdbgcolor="blue"><inputtype="radio"value="blue"name="radio"><br></td>");out.println("<tdbgcolor="pink"><inputtype="radio"value="pink"name="radio"><br></td></tr>");out.println("</tbody></table><inputtype="submit"value="刷新字体颜色"name="button1"></center></form><br> <br>");out.println("<h1>这是一个Cookie的测试程序</h1>");out.println("</font>");out.println("<formmethod="post"name="form2">");out.println("请选择你喜欢的背景颜色");out.println("<center><tableborder="1"align="center">");out.println("<tbody><tr><tdbgcolor="red"><inputtype="radio"name="radio1"value="red"><br></td>");out.println("<tdbgcolor="yellow"><inputtype="radio"value="yellow"name="radio1"><br></td>");out.println("<tdbgcolor="green"><inputtype="radio"value="green"name="radio1"><br></td>");out.println("<tdbgcolor="blue"><inputtype="radio"value="blue"name="radio1"><br></td>");out.println("<tdbgcolor="pink"><inputtype="radio"value="pink"name="radio1"><br></td></tr>");out.println("</tbody></table><inputtype="submit"value="刷新背景颜色"name="button2"></center></form><br> <br>");out.println("<h1>这是一个Cookie的测试程序</h1>");out.println("</font>");out.println("</h2></BODY>");out.println("</HTML>");out.flush();out.close();}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}
解决方案
解决方案二:
很简单的每次打开页面是先取cookie如果有志就选中一致的没有就算了点击保存后将新值写入cookie配合javascript很好搞定的
解决方案三:
麻烦你讲明白点吧我是初学者啊我就想实现上次的设置在下次打开浏览器能显示出来啊我现在用的是两个cookies一个是字体颜色的cookies一个是背景颜色的cookies可是都没有实现我是按照cookies的写法写的可是就是不能显示出来上次的设置