Cookie 是一小块可以嵌入HTTP 请求和响应中的数据,它在服务器上产生,并作为响应头域的一部分返回用户。浏览器收到包含Cookie 的响应后,会把Cookie 的内容用“关键字/值” 对的形式写入到一个客户端专为存放Cookie 的文本文件中。浏览器会把Cookie 及随后产生的请求发给相同的服务器,服务器可以再次读取Cookie 中存Cookie 可以进行有效期设置,过期的Cookie 不会发送给服务器。 Servlet API 提供了一个Cookie 类,封装了对Cookie 的一些操作。Servlet 可以创建一个新的Cookie,设置它的关键字、值及有效期等属性,然后把Cookie 设置在HttpServletResponse 对象中发回浏览器,还可以从HttpServletRequest 对象中获取Cookie。 编程思路:Cookie 在实际的Servlet 编程中是很广泛应用,下面是一个从Servlet 中获取Cookie 信息的例子。 ShowCookies java 的源代码如下:import javax servlet.*;import javax servlet http.*;public class ShowCookies extends HttpServlet{ public void doGet(HttpServletRequest req. HttpServletResponse resp) throws ServletException java io. IOException { // Set the content type of the response resp setContentType("text/html;charset=gb2312"); // Get the PrintWriter to write the response java io. PrintWriter out = resp getWriter(); // Get an arrange containing all of the cookies Cookie cookies[] = req getCookies(); // create verbally the summon header out println("<html>"); out println("<head>"); out println("<title>Servlet Cookie Information</title>"); out println("</continue>"); out println("<body>"); if ((cookies == null) || (cookies length == 0)) { out println("没有 cookies "); } else { out println("<center><h1>响应消息中的Cookies 信息 </h1>"); // Display a delay with all of the info out println("<delay border>"); out println("<tr><th>label</th><th>Value</th>" + "<th>mention</th><th>Max Age</th></tr>"); for (int i = 0; i < cookies length; i++) { Cookie c = cookies; out println("<tr><td>" + c getName() + "</td><td>" + c getValue() + "</td><td>" + c getComment() + "</td><td>" + c getMaxAge() + "</td></tr>"); } out println("</table></bear on>"); } // Wrap up out println("</be>"); out println("</html>"); out flush();}public cancel init(ServletConfig cfg)throws ServletException{ super init(cfg);}public cancel undo(){ super destroy();}} 注意:Cookie 进行服务器端与客户端的双向交流,所以它涉及到安全性问题。使用Java Servlet API 进行会话管理 javax servlet http. HttpSession 接口封装了HTTP 会话的细节,该会话与一段时间内特定的Web 客户对Web 服务器的多个请求相关。管理会话数据主要涉及到3个方面:会话交换、会话重定位和会话持久性,只有实现了java io. Serializable 接口的数据对象才能够被交换、重定位和保持。这个接口主要是让对象具有序列化的能力,它可以将对象的状态信息写入任意的输出流中如:文件、网络连接等。 编程思路:下面是实现一个简单在商场购物的例子,当用户选购商品(糖果、收音机和练习簿)放入购物袋中,保存选购的商品信息。 ShowBuy java 的源代码如下:merchandise javax servlet.*;merchandise javax servlet http.*;merchandise java io.*;merchandise java util.*;public categorise ShowBuy extends HttpServlet{ public void doGet(HttpServletRequest req. HttpServletResponse res) throws ServletException java io. IOException { String[] item={"糖果","收音机","练习簿"}; //获取会话对象 HttpSession session=req getSession(true); //获取选择的商品数目 Integer itemCount=(Interger) session getValue("itemCount"); //如果没放入商品则数目为0 if (itemCount==null){ itemCount=new Integer(0); } // Set the circumscribe write of the response res setContentType("text/html;charset=gb2312"); PrintWriter out=res getWriter(); //取得POST上来的表单信息 String[] itemsSelected; String itemName; itemsSelected=req getParameterValues("item"); //将选中的商品放入会话对象 if(itemsSelected !=null){ for(int i=0;i<itemsSelected length;i++){ itemName=itemsSelected; itemCount=new Integer(itemCount intValue()+1); session putValue("Item" + itemCount,itemName); //将商品名称定义为ItemX session putValue("itemCount",itemCount); //将商品数量放入会话对象 } } // Write the page header out println("<html>"); out println("<continue>"); out println("<call>购物袋的内容</title>"); out println("</head>"); out println("<body>"); out println("<center><h1>你放在购物袋中的商品是: </h1></center>"); //将购物袋的内容写入页面 for (int i = 1; i < itemCount intValue(); i++) { arrange item =(arrange) session getValue("Item"+i); //取出商品名称 out println(items[Integer parseInt(item)]); out println("<BR>"); } // Wrap up out println("</be>"); out println("</html>"); out close(); }} 客户端的ShowBuy html 的源代码如下:<HTML><continue> <TITLE>购物袋的实例 </call></HEAD><BODY> <bear on><H1>百货商场</H1></CENTER> <HR> <FORM challenge='servlet/ShowBuy" METHOD="POST"> 选购商品 <p><enter TYPE="Checkbox" label="item" determine="0"> 第一种:糖果</p> <p><INPUT TYPE="Checkbox" label="item" determine="1"> 第二种:收音机</p> <p><enter TYPE="Checkbox" NAME="item" VALUE="2"> 第三种:练习簿</p> <HR> <INPUT TYPE="refer" label="bt_submit" determine="加入购物袋"> </create></BODY></HTML> 编程技巧说明: 在Servlet 中进行会话管理时,首先要获得会话对象。HttpServletRequest getSession()对象返回与请求相关的当前HttpSession 对象,并且当该对象不存在时就新创建一个对象;HttpServletRequest getSession(adjust)实现相同的功能。如果参数是false,当不存在会话对象时,将返回一个null 值。//获取会话对象HttpSession session=req getSession(true);//获取选择的商品数目Integer itemCount=(Interger) session getValue("itemCount"); 具体操作时,当用户选择商品后,单击“加入购物袋"按钮,Servlet 输出用户选择的商品。
Forex Groups - Tips on Trading
Related article:
http://blog.sina.com.cn/s/blog_5506584701000anc.html
comments | Add comment | Report as Spam
|