标签:
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
标签:
原文地址:http://www.cnblogs.com/daxiong225/p/4772984.html