程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Web開辟之基於Session的購物市肆完成辦法

Java Web開辟之基於Session的購物市肆完成辦法

編輯:關於JAVA

Java Web開辟之基於Session的購物市肆完成辦法。本站提示廣大學習愛好者:(Java Web開辟之基於Session的購物市肆完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Web開辟之基於Session的購物市肆完成辦法正文


本文實例講述了Java Web開辟之基於Session的購物市肆完成辦法。分享給年夜家供年夜家參考,詳細以下:

package cn.com.shopping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 BuyServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id=request.getParameter("id");
    Book book=(Book)Db.getAll().get(id);
    //再加上誰人封閉Cookie時session的剞劂計劃
    //阻攔session的時刻處理計劃
    HttpSession session=request.getSession(false);
    //從session中獲得用戶的保留一切書的聚集(購物車)
    List list=(List)session.getAttribute("list");
    if(list==null)
    {
      list=new ArrayList();
      session.setAttribute("list", list);
    }
    list.add(book);
    String url=response.encodeRedirectURL("/Session/SessionCountDemo");
    response.sendRedirect(url);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
}
package cn.com.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
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 ListBookServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    HttpSession session=request.getSession();
    out.print("本店有以下的商品:<br/>");
    Map<String ,Book > map=Db.getAll();
    for(Map.Entry<String, Book> entry:map.entrySet())
    {
      Book book=entry.getValue();
      String url=response.encodeURL("/Session/BuyServlet?id="+book.getId());
      out.print(book.getName()+"<a href='"+url+"' target='_blank' >購置</a><br/>");
    }
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
}
//Db作為數據庫
class Db
{
  private static Map<String ,Book> map=new LinkedHashMap();
  static
  {
    map.put("1", new Book("1","Java WEB開辟","WY","好書"));
    map.put("2", new Book("2","WEB開辟","zt","普通"));
    map.put("3", new Book("3","法式設計","df","較好書"));
    map.put("4", new Book("4","盤算機構成","as","普通好書"));
    map.put("5", new Book("5","編譯道理","ty","很好書"));
    map.put("6", new Book("6","收集保護","hj","異常好書"));
  }
  public static Map getAll()
  {
    return map;
  }
}
//書
class Book
{
  private String id;
  private String name;
  private String author;
  private String description;
  public Book() {
    super();
    // TODO Auto-generated constructor stub
  }
  public Book(String id, String name, String author, String description) {
    super();
    this.id = id;
    this.name = name;
    this.author = author;
    this.description = description;
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getAuthor() {
    return author;
  }
  public void setAuthor(String author) {
    this.author = author;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
}
package cn.com.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 SessionCountDemo extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    HttpSession session=request.getSession();
    if(session==null)
    {
      out.write("您沒買任何的商品!");
      return;
    }
    out.write("您購置了以下的商品:");
    List<Book> list=(List) session.getAttribute("list");
    for(Book book:list)
    {
      out.write(book.getName());
    }
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
}

願望本文所述對年夜家Java web法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved