欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Web開發(fā)之基于Session的購物商店實(shí)現(xiàn)方法

 更新時間:2015年10月19日 14:49:49   作者:煙大洋仔  
這篇文章主要介紹了Java Web開發(fā)之基于Session的購物商店實(shí)現(xiàn)方法,涉及Java針對session的操作及數(shù)據(jù)庫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了Java Web開發(fā)之基于Session的購物商店實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

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);
    //再加上那個關(guān)閉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作為數(shù)據(jù)庫
class Db
{
  private static Map<String ,Book> map=new LinkedHashMap();
  static
  {
    map.put("1", new Book("1","Java WEB開發(fā)","WY","好書"));
    map.put("2", new Book("2","WEB開發(fā)","zt","一般"));
    map.put("3", new Book("3","程序設(shè)計","df","較好書"));
    map.put("4", new Book("4","計算機(jī)組成","as","一般好書"));
    map.put("5", new Book("5","編譯原理","ty","很好書"));
    map.put("6", new Book("6","網(wǎng)絡(luò)維護(hù)","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程序設(shè)計有所幫助。

相關(guān)文章

  • springboot動態(tài)調(diào)整日志級別的操作大全

    springboot動態(tài)調(diào)整日志級別的操作大全

    這篇文章主要介紹了springboot動態(tài)調(diào)整日志級別的方法,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • springboot整合skywalking的使用詳解

    springboot整合skywalking的使用詳解

    隨著分布式應(yīng)用大規(guī)模部署,應(yīng)用可觀測性從理論到落地已經(jīng)在眾多大型互聯(lián)網(wǎng)應(yīng)用中得到實(shí)踐,比如針對日志可視化ELK解決方案,分布式鏈路追蹤APM解決方案SkyWalking等,今天將詳細(xì)介紹下APM解決方案中一款重要工具SkyWalking的使用,需要的朋友可以參考下
    2024-01-01
  • java并發(fā)數(shù)據(jù)包Exchanger線程間的數(shù)據(jù)交換器

    java并發(fā)數(shù)據(jù)包Exchanger線程間的數(shù)據(jù)交換器

    這篇文章主要為大家介紹了java并發(fā)數(shù)據(jù)包使用數(shù)據(jù)交換器Exchanger來進(jìn)行線程之間的數(shù)據(jù)交換。有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • Java簡易登錄注冊小程序

    Java簡易登錄注冊小程序

    這篇文章主要為大家詳細(xì)介紹了Java圖形界面開發(fā),簡易登錄注冊小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • spring-data-redis連接操作redis的實(shí)現(xiàn)

    spring-data-redis連接操作redis的實(shí)現(xiàn)

    spring-data-redis則是對Jedis進(jìn)行了高度封裝,使用起來非常方便。本文主要介紹了spring-data-redis連接操作redis的實(shí)現(xiàn),感興趣的可以了解一下
    2021-07-07
  • tk.mybatis實(shí)現(xiàn)uuid主鍵生成的示例代碼

    tk.mybatis實(shí)現(xiàn)uuid主鍵生成的示例代碼

    本文主要介紹了tk.mybatis實(shí)現(xiàn)uuid主鍵生成的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時任務(wù)的示例

    Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時任務(wù)的示例

    這篇文章主要介紹了Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時任務(wù)的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫

    使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫

    這篇文章主要介紹了使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Java clone方法詳解及簡單實(shí)例

    Java clone方法詳解及簡單實(shí)例

    這篇文章主要介紹了 Java clone方法詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動方法

    使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動方法

    這篇文章主要介紹了使用java web 在jsp文件及Class中連接MySQL和SQLserver的驅(qū)動方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10

最新評論