Servlet中Writer和Cookie共同使用的注意事项
来源:互联网 发布:粮库远程监控软件 编辑:程序博客网 时间:2024/05/16 17:06
闲来无事打算写个servlet,myeclipse自动生成了下面的代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();String userName = request.getParameter("username");System.out.println("the name you get is :" + userName);Cookie cookie = new Cookie("userName", userName);response.addCookie(cookie);}
再添上我想做的事情:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close(); String userName = request.getParameter("username"); System.out.println("the name you get is :" + userName); Cookie cookie = new Cookie("userName", userName); response.addCookie(cookie); }
测试居然没有任何效果,cookie没有设置上。经过反复研究发现把这几句提前到flush()方法之前就好使了。仔细想想其中的原因,应该是这样的:
1.cookie必须随response的头信息传递,否则是无效的;
2.out对象是缓存的,所以response中设置cookie可以放在out.println(XXXXX)方法的后面;
3.调用flush()方法时候,将信息发送回浏览器,在这之前如何设置了cookie,则response会将这个信息放置在头信息中返回,浏览器收到后就存下来了;
4.如果将设置cookie的代码放在flush之后,因为信息已经发走了,所以无法再在头信息中设置值了。所以就无效了。
5.不过,flush之后仍可以向前台页面传送东西,这个是没问题的。
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.flush();out.println("<div> 1234567890 </div>");out.println(" </BODY>");out.println("</HTML>");out.close();}
这样获取的页面是完整的,显示没有任何问题。
- Servlet中Writer和Cookie共同使用的注意事项
- php cookie 的使用和注意事项
- 关于Cookie的使用案例和注意事项
- Cookie的使用注意事项
- servlet cookie的使用
- servlet学习之servlet中cookie和session的介绍
- Yii2使用Cookie的注意事项
- Yii2使用Cookie的注意事项
- JSP中使用cookie的方法以及注意事项总结
- cookie的使用方法和注意事项
- Servlet中Cookie的用法
- Cookie 的使用(Servlet)
- express中cookie的使用和cookie-parser的解读
- 在Servlet程序中使用Cookie
- cookie是什么?php中cookie的设置和使用
- windows live writer的安装和使用
- servlet中写入和读取删除cookie
- servlet中cookie和session操作
- 软核,硬核、固核的区别!(整理总结)
- POJ 3013 Big Christmas Tree 最短路
- Google发布了一款名为J2ObjC的源代码语言转换工具
- 《Description of CWnd derived MFC objects and multithreaded applications in Visual C++》译文
- EL表达式详解
- Servlet中Writer和Cookie共同使用的注意事项
- linux下usb转串口设置
- 解决win7英文版下中文显示乱码问题
- Linux下USB转串口的驱动
- CListCtrl全选与反选
- Highcharts 时间截断 利用主轴可以做上下限设定---不是很可取 上下限设定---最佳可取方案
- 3G USB modem 驱动移植总结
- hdu 2602 Bone Collector 简单dp题 0-1背包
- Pascal教学