码迷,mamicode.com
首页 > 其他好文 > 详细

Session累计用户数据列表

时间:2015-08-31 15:12:41      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

OrderForm.html

<body>
  <center>
  <h1 ><font size="20">Order Items</font></h1>
    <form action="./showItems">
    new Item order:<input type="text" name="item" value="yache"/><br>
    <input type="submit" value="show all purched">    
    </form>
    </center>
  </body>

 

 

package cn.donghua.session;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class ShowItems extends HttpServlet {

 @Override  protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException {   

resp.setContentType("text/html");   

PrintWriter out = resp.getWriter();   

out.write("<p>item purchased</p>");  

  HttpSession session = req.getSession();  

  System.out.println(session.getId());   

ArrayList preItems = (ArrayList) session.getAttribute("preItems");   

if(preItems==null){    

  preItems = new ArrayList();    

  session.setAttribute("preItems", preItems);   

}   

String item = req.getParameter("item");   

synchronized (preItems) {   

   if((item!=null)&&(!item.trim().equals(""))){     

  preItems.add(item);    

  }   

   if(preItems.size()!=0){     

  for(int i=0;i<preItems.size();i++){      

  item = (String) preItems.get(i);     

   out.write("<p>item "+i+"\t"+item+"</p>");     

  }    

}  

 else{     

out.write("there is no items");    

}   

}        

}

 @Override  protected void doPost(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException {   

doGet(req,resp);  

}  

}

没有输入时:

  item purchased

  there is no items

有输入时:

item purchased

item 0 item1

item 1 item1

item 2 item3

 

Session累计用户数据列表

标签:

原文地址:http://www.cnblogs.com/daxiong225/p/4772984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!