Chapter 6. http..? ID. ID....... ecrm(ebusiness )., ecrm.. Cookie.....,. 20, 300 4. JSP. Cookie API javax.servlet.http. 3.
1. 2. 3. API. Cookie(String name, String value). name value. setxxx. getxxx. public int getmaxage() :.. public void setmaxage(int MaxAge) :.. 1. public String getdomain() :. public void setdomain(string Domain) :.. setdomain.. 2..yahoo.com yahoo.com. public String getpath() : path. public void setpath(string path) : path, path path path., jsp.boolpae.com/jsp/example.jsp jsp jsp.boolpae.com/xml. setpath( / ). / public void setvalue(string value) : value().. public String getvalue() :.
Example. JSP. List 6.1 setcookie.jsp <HTML> <HEAD> <TITLE> Set Cookie</TITLE> </HEAD> <BODY> <% for(int i=0;i<5;i++){ Cookie c = new Cookie("First Cookie"+i, "Value"+i); c.setmaxage(5); response.addcookie(c); c = new Cookie("Second Cookie"+i, "Value"+i); c.setmaxage(60*60); response.addcookie(c); %> <center> <h1> Cookie Example </h1> 5 10.<br> 5 5, 5 1. <hr> <a href="showcookie.jsp">cookie </a> </BODY> </HTML> List 6.1.
List 6.2 showcookie.jsp <HTML> <HEAD> <TITLE> show Cookie </TITLE> </HEAD> <BODY> <center> <h1> Cookie Example </h1> <table border=1> <tr><td bgcolor="gray"> </td> <td bgcolor="gray"></td></tr> <% Cookie[] c = request.getcookies(); for(int i=0;i<c.length;i++){ %> Cookie cookie = c[i]; <tr><td><%=cookie.getname()%></td><td><%=cookie.getvalue()%></tr> <% %> </table> </BODY>
</HTML> List 6.2 JSP. Cookie request.getcookies(). getname getvalue., 5. 5 5 5. [ ]
Second Cookie., JSESSIONID ID., ID. CookieUtil. List 6.3 CookieUtil.java package com.boolpae.jsp; import javax.servlet.http.cookie; public class CookieUtil{ public static String getvalue(cookie[] cookies, String name){ for(int i=0;i<cookies.length;i++){ if(name.equals(cookies[i].getname()))
return cookies[i].getvalue(); return null; public static Cookie getcookie(cookie[] cookies, String name){ for(int i=0;i<cookies.length;i++){ if(name.equals(cookies[i].getname())) return cookies[i]; return null; Cookie Cookie (value). static. JSP. List 6.4 CookitUtilExample.jsp <%@ page import="com.boolpae.jsp.cookieutil" %> <% Cookie c = new Cookie("Book", "JavaServer Pages"); response.addcookie(c); c = new Cookie("Jazz","BlueNote"); response.addcookie(c); %> <HTML> <HEAD> <TITLE> CookieUtilTest </TITLE> </HEAD> <BODY> <center> <h1>cookieutil Example</h1> <%= CookieUtil.getValue(request.getCookies(), "Book") %><br> <%= CookieUtil.getValue(request.getCookies(), "Jazz") %> </BODY> </HTML>
. null. Cookie. response Header. refresh... List 6.5 BlueCookie.java package com.boolpae.jsp; import javax.servlet.http.cookie; public class BlueCookie extends Cookie{ public static final String path = "/"; public BlueCookie(String name, String value, int maxage){ super(name, value); setmaxage(maxage); setpath(path);
maxage, path /. CookieUtilExample.jsp. List 6.6 CookieUtilExample.jsp <%@ page import="com.boolpae.jsp.*"%> <% Cookie c = new Cookie("Book", "JavaServer Pages"); response.addcookie(c); c = new Cookie("Jazz","BlueNote"); response.addcookie(c); c = new BlueCookie("Movie","JSA", 5); response.addcookie(c); %> <HTML> <HEAD> <TITLE> CookieUtilTest </TITLE> </HEAD> <BODY> <center> <h1>cookieutil Example</h1> <%= CookieUtil.getValue(request.getCookies(), "Book") %><br> <%= CookieUtil.getValue(request.getCookies(), "Jazz") %><br> <%= CookieUtil.getValue(request.getCookies(), "Movie") %> </BODY> </HTML>. 5 refresh Movie null.
response. addcookie. maxage 0 addcookie.. Cookie c = new Cookie( xxx, yyy ); response.addcookie(c); c = new Cookie( xxx, yyy ); c.setmaxage(0); response.addcookie(c); (?)maxage0. Session Http. Cookie.? URL-rewriting Hidden Form session. URL-rewriting sessionid, Hidden Form HTML form <input type= hidden name= name value= value > GET POST.
. Session. Session API Session javax.servlet.http.httpsession. HttpSession. public Object getattribute(string name) name value. String session Object. API 2.1. getvalue, 2.2 getattribute. getvalue.(aip deprecated ) pubic void setattribute(string name, Object value). Object. putvalue getvalue deprecated. public void removeattribute(string name). removevalue deprecated.. public Enumeration getattributenames() name Enumeration. public String getid() ID. ID. public long getcreationtime() long. millisecond. 197011 millisecond. public void invalidate().. public boolean isnew() boolean. invalidate
isnew true. Example... JSP JSP.,.,..., ( ). JSP Books JSP. CartPage JSP. CartPage.jsp. CartPage.jsp. (?) Request JSP. Cartpage.jsp... CartPage.jsp.. Book. List 6.7 Book.java package com.boolpae.jsp; public class Book{ private String isbn; private String title; private int price; public Book(String isbn, String title, int price){
this.isbn = isbn; this.title = title; this.price = price; public String getisbn(){ return isbn; public String gettitle(){ return title; public int getprice(){ return price;. isbn,, ( ).. BookList, BookList. List 6.8 BookList.java package com.boolpae.jsp; import java.util.vector; public class BookList{ private static Vector books= new Vector(); static{ books.addelement(new Book("A010101", "JavaServer Pages by ", 100 )); books.addelement(new Book("A020202", "JavaServer Pages by ", 40 )); public static Vector getbooks(){ return books;
public static Book getbook(string isbn){ if(isbn == null) return null; for(int i=0;i<books.size();i++){ Book book = (Book)books.elementAt(i); if(isbn.equals(book.getisbn())){ return book; return null; BookList. Book Book. static. static books. static static{. Vector ISBN ISBN Book.. List 6.9 Order.java package com.boolpae.jsp; public class Order{ private Book book; private int count = 1; public Order(Book book){ this.book = book; public Book getbook(){ return book; public void pluscount(){ count++; public void setcount(int c){ this.count = c; public int getcount(){ return count;
public int gettotalprice(){ return book.getprice()*count; Order. Book (count). 1 1 pluscount setcount. (Cart). List 6.10 Cart.java package com.boolpae.jsp; import java.util.vector; public class Cart{ private Vector ordered; public Cart(){ ordered = new Vector(); public Vector getordered(){ return ordered; public void addbook(string isbn){ for(int i=0;i<ordered.size();i++){ Order order = (Order)ordered.elementAt(i); if(isbn.equals(order.getbook().getisbn())){ order.pluscount(); return; Order neworder = new Order(BookList.getBook(isbn)); ordered.addelement(neworder); public void setcount(string isbn, int count){ Order order; for(int i=0;i<ordered.size();i++){ order = (Order)ordered.elementAt(i); if(isbn.equals(order.getbook().getisbn())){ if(count<=0){ ordered.removeelementat(i); else{ order.setcount(count); return;
Order neworder = new Order(BookList.getBook(isbn)); ordered.addelement(neworder);. Vector ordered Order Vector.. addbook, setcount., addbook..? addbook ISBN 1 ( -Order pluscount ),.(ordered Book addelement ) setcount. ISBN ISBN.( -Order setcount ) 0 0...,.. URL... JSP.. List 6.11 Books.jsp <%@ page import="java.util.vector" contenttype="text/html;charset=euc- KR" %> <HTML> <HEAD> <TITLE> Session Example </TITLE> <style> BODY, PRE {font-family:tahoma, ; font-size:8pt.blue{color:blue; font-size:15pt </style>
</HEAD> <BODY> <center> <h1> Shopping Cart Example </h1> <hr> <pre> <% Vector books = com.boolpae.jsp.booklist.getbooks(); for(int i=0;i<books.size();i++){ com.boolpae.jsp.book book = (com.boolpae.jsp.book)books.elementat(i); %> <font class= blue><%= book.gettitle()%></font><p> : <%=book.getprice()%> / ISBN: <%=book.getisbn()%> <form method='get' action='cartpage.jsp'> <input type='hidden' name="isbn" value='<%=book.getisbn()%>'> <input type='submit' value=' '> </form> <hr> <% %> </BODY> </HTML> JSP. 2. <form> CartPage.jsp. Form hidden ISBN. Book (!). page import.. List 6.11. [Books.jsp]
JSP.. ISBN CartPage.jsp. List 6.12 CartPage.jsp <%@ page import="java.util.vector" contenttype="text/html;charset=euc- KR" %> <jsp:usebean id="cart" class="com.boolpae.jsp.cart" scope="session" /> <HTML> <HEAD> <TITLE> add Cart </TITLE> </HEAD> <BODY> <center> <h1> Example</h1> <% String requestisbn = request.getparameter("isbn"); String requestcount = request.getparameter("count");
if(requestisbn!= null){ if(requestcount == null){ cart.addbook(requestisbn); else{ int parsecount; try{ parsecount = Integer.parseInt(requestCount); catch(numberformatexception e){ parsecount = 1; cart.setcount(requestisbn, parsecount); Vector ordered = cart.getordered(); if(ordered.size() == 0){ out.print("<h1> </h1>"); else{ out.print("<table border=1><tr><td>isbn</td><td> </td><td> </td><td> </td><td> </td>"); for(int i=0;i<ordered.size();i++){ com.boolpae.jsp.order order = (com.boolpae.jsp.order)ordered.elementat(i); int count = order.getcount(); com.boolpae.jsp.book book = order.getbook(); String isbn = order.getbook().getisbn(); String title = order.getbook().gettitle(); int price = book.getprice(); int totalprice = order.gettotalprice(); %> <tr> <td><%=isbn%></td> <td><%=title%></td> <td><%=price%></td> <td><form method="get" action="cartpage.jsp"> <input type="hidden" name="isbn" value="<%=isbn%>"> <input type="text" size="5" maxlength="2" name="count" value="<%=count%>"> <input type="submit" value=" "> </form> </td> <td><font color="red"><%=totalprice%></font></td> </tr> <% //end of for %> </table> : 1. <% //end of if %> </BODY> </HTML>
<jsp:usebean>. Session Session. <jsp:usebean id= cart class= com.boolpae.jsp.cart scope= session />? Cart. <% com.boolpae.jsp.cart cart = new com.boolpae.jsp.cart(); session.setattribute( cart, cart); %>.... String requestisbn = request.getparameter("isbn"); String requestcount = request.getparameter("count"); if(requestisbn!= null){ if(requestcount == null){ cart.additem(requestisbn); else{ int parsecount; try{ parsecount = Integer.parseInt(requestCount); catch(numberformatexception e){ parsecount = 1; cart.setcount(requestisbn, parsecount); isbn count. Books.jsp ISBN. cart.addbook. requestcount null Books.jsp. count null. null cart.setcount. requestisbn parsecount setcount int. request String int Integer.parseInt. int ( int ) NumberFormatException. Count1. parsecount setcount. 0.. <form> text
. form (count)isbn. Book Order. JavaServer Pages. [CartPage.jsp - 1],. 40 99. [CartPage.jsp 2]
... List 6.13 UserList.java package com.boolpae.jsp; import java.util.*; public class UserList{ private static Vector uservector = new Vector(); public static void adduser(string username){ String temp; for(int i=0;i<uservector.size();i++){ temp = (String)userVector.elementAt(i); if(username.equals(temp)){ return; uservector.addelement(username); public static void removeuser(string user){
String temp; for(int i=0;i<uservector.size();i++){ temp = (String)userVector.elementAt(i); if(user.equals(temp)){ uservector.removeelementat(i); return; public Vector getuser(){ return uservector; List 6.13. Vector, Vector. List 6.14. List 6.14 User.java package com.boolpae.jsp; import javax.servlet.http.*; public class User implements HttpSessionBindingListener{ private String username; public void setusername(string username){ this.username = username; UserList.addUser(userName); public void valuebound(httpsessionbindingevent event){ // UserListBean.addUser(userName); public void valueunbound(httpsessionbindingevent event){ UserList.removeUser(userName); javax.servlet.http.httpsessionbindinglistener. valuebound valueunbound.. Vector
Vector. valueunbound. jsp. List 6.15 UserList.jsp <HTML> <BODY> <H1>User List example</h1> <jsp:usebean id="userlist" class="com.boolpae.jsp.userlist" scope="application" /> <jsp:usebean id="user" class="com.boolpae.jsp.user" scope="session" /> <jsp:setproperty name="user" property="username" value="bluenote" /> <% java.util.vector uservector = userlist.getuser(); for(int i=0;i<uservector.size();i++){ out.print(uservector.elementat(i)); %> </BODY> </HTML>. List 6.15 UserList.jsp <jsp:setproperty>value.. BlueNote. value. 1 refresh. 1. jsp web.xml 1. List 6.16web.xml
. List 6.16 web.xml <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <session-config> <session-timeout> 1 </session-timeout> </session-config> </web-app> web.xml WEB-INF web.xml. JSP WEB-INF, META-INF. WEB-INF web.xml. jsp:setproperty value.