세션 대상의 응용: 간단한 카트 기능 구현

17667 단어 session
ShowAllBooksServlet.java 파일:
package com.itheima.session.app1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// , 
// 
public class ShowAllBooksServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.getSession();

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        // , 
        out.write("

이 사이트에는 다음과 같은 좋은 책이 있습니다.

"
); Map books = BookDB.findAllBooks(); for(Map.Entry me:books.entrySet()){ String url = request.getContextPath()+"/servlet/BuyServlet?id="+me.getKey(); url = response.encodeURL(url); out.write(me.getValue().getName()+"  구입
"
); } // String url = request.getContextPath()+"/servlet/ShowCartServlet"; url = response.encodeURL(url); out.write("카트 보기"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

ShowCartServlet.java 파일:
package com.itheima.session.app1;

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 ShowCartServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        List cart = (List)session.getAttribute("cart");
        if(cart==null||cart.size()==0){
            out.write(" ");
            return;
        }

        out.write("

당신이 구매한 상품은 아래와 같습니다

"
); for(Book b:cart){ out.write(b.getName()+"
"
); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

BuyServlet.java 파일:
package com.itheima.session.app1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
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 {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        Book b = BookDB.findById(id);

        // HttpSession 》 List 
        HttpSession session = request.getSession();
        List cart = (List) session.getAttribute("cart");
        if(cart==null){
            cart = new ArrayList();
            session.setAttribute("cart", cart);
        }
        // 
        cart.add(b);
        String url = request.getContextPath()+"/servlet/ShowAllBooksServlet";
        url = response.encodeURL(url);
        out.write(b.getName()+" 쇼핑 계속");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

Book.java 파일:
package com.itheima.session.app1;

import java.io.Serializable;

public class Book implements Serializable{
    private String id;
    private String name;
    private String price;
    private String author;
    private String 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 getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    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;
    }
    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + ", price=" + price
                + ", author=" + author + ", description=" + description + "]";
    }
    public Book(String id, String name, String price, String author,
            String description) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.author = author;
        this.description = description;
    }
    public Book(){}
}

BookDB.java 파일:
package com.itheima.session.app1;

import java.util.HashMap;
import java.util.Map;

// 
public class BookDB {
    //key: id  value:id 
    private static Map books = new HashMap();
    static{
        books.put("1", new Book("1", " ", "10", " ", " , "));
        books.put("2", new Book("2", " ", "2", " ", " , "));
        books.put("3", new Book("3", " ", "20", " ", " "));
        books.put("4", new Book("4", " ", "10", " ", " , "));
        books.put("5", new Book("5", "JavaWeb ", "100", " ", " "));
    }
    public static Map findAllBooks(){
        return books;
    }
    public static Book findById(String bookId){
        return books.get(bookId);
    }
}

실행:http://localhost:8080/Session/servlet/ShowAllBooksServlet결과:당신이 구매한 상품은 다음과 같습니다.
욕녀심경규화보전JavaWeb 개발

좋은 웹페이지 즐겨찾기