每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐Java Servlet 编程及应用之五,赶紧看过来吧!
【 tulaoshi.com - 编程语言 】
Cookie 是一小块可以嵌入HTTP 请求和响应中的数据,它在服务器上产生,并作为响应头域的一部分返回用户。浏览器收到包含Cookie 的响应后,会把Cookie 的内容用“关键字/值” 对的形式写入到一个客户端专为存放Cookie 的文本文件中。浏览器会把Cookie 及随后产生的请求发给相同的服务器,服务器可以再次读取Cookie 中存Cookie 可以进行有效期设置,过期的Cookie 不会发送给服务器。import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>This is a simple servlet that displays all of the
* Cookies present in the request
*/
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 array containing all of the cookies
Cookie cookies[] = req.getCookies();
// Write the page header
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Cookie Information</title>");
out.println("</head>");
out.println("<body>");
if ((cookies == null) || (cookies.length == 0)) {
out.println("没有 cookies ");
}
else {
out.println("<center><h1>响应消息中的Cookies 信息 </h1>");
// Display a table with all of the info
out.println("<table border>");
out.println("<tr><th>Name</th><th>Value</th>" + "<th>Comment</th><th>Max Age</th></tr>");
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
out.println("<tr><td>" + c.getName() + "</td><td>" +
c.getValue() + "</td><td>" + c.getComment() + "</td><td>" + c.getMaxAge() + "</td></tr>");
}
out.println("</table></center>");
}
// Wrap up
out.println("</body>");
out.println("</html>");
out.flush();
}
/**
* <p>Initialize the servlet. This is called once when the
* servlet is loaded. It is guaranteed to complete before any
* requests are made to the servlet
* @param cfg Servlet configuration information
*/
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
/**
* <p>Destroy the servlet. This is called once when the servlet
* is unloaded.
*/
public void destroy()
{
super.destroy();
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class 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 content type 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[i];
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("<head>");
out.println("<title>购物袋的内容</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><h1>你放在购物袋中的商品是: </h1></center>");
//将购物袋的内容写入页面
for (int i = 1; i < itemCount.intValue(); i++) {
String item =(String) session.getValue("Item"+i);
//取出商品名称
out.println(items[Integer.parseInt(item)]);
out.println("<BR>");
}
// Wrap up
out.println("</body>");
out.println("</html>");
out.close();
}
}
<HTML>
<HEAD>
<TITLE>购物袋的实例 </TITLE>
</HEAD>
<BODY>
<CENTER><H1>百货商场</H1></CENTER>
<HR>
<FORM ACTION=servlet/ShowBuy" METHOD="POST">
选购商品
<p><INPUT TYPE="Checkbox" NAME="item" VALUE="0">
第一种:糖果</p>
<p><INPUT TYPE="Checkbox" NAME="item" VALUE="1">
第二种:收音机</p>
<p><INPUT TYPE="Checkbox" NAME="item" VALUE="2">
第三种:练习簿</p>
<HR>
<INPUT TYPE="Submit" NAME="bt_submit" VALUE="加入购物袋">
</FORM>
</BODY>
</HTML>
//获取会话对象
HttpSession session=req.getSession(true);
//获取选择的商品数目
Integer itemCount=(Interger) session.getValue("itemCount");
来源:http://www.tulaoshi.com/n/20160219/1599446.html
看过《Java Servlet 编程及应用之五》的人还看了以下文章 更多>>