Implementation of the session-based shopping store for Java Web development _java

Source: Internet
Author: User

This article describes a session-based shopping store implementation for Java Web development. Share to everyone for your reference, specific as follows:

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;
  Complete purchase 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);
    The solution HttpSession Session=request.getsession (FALSE) when the session's 剞 something bad behind scheme//block session is blocked when the cookie is closed.
    From the session to get the user to save all the books collection (shopping cart) 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;
  Display book 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 ("Our store is like the merchandise:<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 ' > Buy </a><br/>"); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
  On {doget (request,response);
  }//db as database class Db {private static map<string,book> map=new Linkedhashmap ();
    static {Map.put ("1", New book ("1", "Java Web Development", "WY", "Good Books"));
    Map.put ("2", New book ("2", "Web Development", "ZT", "General"));
    Map.put ("3", New book ("3", "Program Design", "DF", "Better Books"));
    Map.put ("4", New book ("4", "Computer composition", "as", "General Good Books"));
    Map.put ("5", New book ("5", "Principles of compilation", "Ty", "very Good books"));
  Map.put ("6", New book ("6", "Network Maintenance", "HJ", "very Good books"));
  public static map GetAll () {return map;
  }//Books 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 ("You did not buy any goods!")
      ");
    Return
    Out.write ("You have purchased the following goods:");
    list<book> list= (list) session.getattribute ("list");
    for (book book:list) {out.write (Book.getname ()); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
  On {doget (request,response);

 }
}

I hope this article will help you with your Java Web programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.