티스토리 뷰

[펌]

http://devyongsik.tistory.com/290

 

 

(예제)

//SessionTest3.java

@WebServlet("/sessionTest3")
public class SessionTest3 extends HttpServlet{
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  HttpSession session = request.getSession();
  String name = request.getParameter("fruit");
  int number = Integer.parseInt(request.getParameter("number"));
  ArrayList<Fruit> cart = null;
  if((cart = (ArrayList<Fruit>)session.getAttribute("cart"))==null){
   cart = new ArrayList<Fruit>();
   cart.add(new Fruit(name,number));
  }else{
   if(cart.contains(new Fruit(name,number))){
    System.out.println("d");
    cart.get(cart.indexOf(new Fruit(name,number))).add(number);
   }else{
    System.out.println("d");
    cart.add(new Fruit(name,number));
   }
  }
  session.setAttribute("cart", cart); 
  response.sendRedirect("fruit2.html");} 
}

 

//ShowCart2.java

@WebServlet("/showCart2")
public class ShowCart2 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();
  ArrayList<Fruit> list = (ArrayList<Fruit>) session.getAttribute("cart");
  if (list != null) {
   for (Fruit f : list) {
    out.print(f.getName()+"("+f.getPrice()+"원):"+f.getNumber()+"개:총"+f.getPrice()*f.getNumber()+"<br>");
   }
  }
  out.close();
 }
}

//fruit2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/edu/sessionTest3">
 <input type="radio" name="fruit" value="apple" checked>사과(500원)<br>
 <input type="radio" name="fruit" value="peach">복숭아(600원)<br>
 <input type="radio" name="fruit" value="grape">포도(1000원)<br>
 <input type="radio" name="fruit" value="watermelon">수박(3000원)<br>
 <input type="radio" name="fruit" value="plum">자두(200원)<br>
 수량<input type="number" name="number" min="1" max="50" required><br>
 <input type="submit" value="장바구니 담기"> 
</form>
<form action="/edu/showCart2">
 <input type="submit" value="장바구니 확인">
</form>
</body>
</html>

//Fruit.java


public class Fruit {
 private String name;
 private int price;
 private int number;

 public Fruit(String name, int number) {
  this.name = name;
  if (name.equals("apple"))
   this.price = 500;
  else if (name.equals("peach"))
   this.price = 600;
  else if (name.equals("grape"))
   this.price = 1000;
  else if (name.equals("watermelon"))
   this.price = 3000;
  else
   this.price = 200;
  this.number = number;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPrice() {
  return price;
 }

 public void setPrice(int price) {
  this.price = price;
 }

 public int getNumber() {
  return number;
 }

 public void setNumber(int number) {
  this.number = number;
 }

 public void add(int number) {
  this.number += number;
 }

 @Override
 public boolean equals(Object obj) {
  Fruit f = (Fruit)obj;
  if(f.getName().equals(this.name)) return true;
  else return false;
 }
 


}

'Java' 카테고리의 다른 글

try catch finally 실행 순서  (0) 2017.02.01
String==null 과 String.isEmpty() 의 차이  (0) 2017.01.24
java.io  (0) 2017.01.13
TreeSet, TreeMap을 위한 Comparable 인터페이스  (0) 2017.01.13
Collection(컬렉션)  (0) 2017.01.13
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함