Java는 최근에 본 책을 표시하기 위해 쿠키를 사용합니다.
1.ben 가방
import java.io.Serializable;
public class Book implements Serializable {
private String id;
private String name;
private String price;
private String auth;
private String publish;
private String description;
public Book() {
}
public Book(String id, String name, String price, String auth,
String publish, String description) {
super();
this.id = id;
this.name = name;
this.price = price;
this.auth = auth;
this.publish = publish;
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
}
2. Dao 가방
import java.util.LinkedHashMap;
import java.util.Map;
import cn.huiyu.ben.Book;
public class BookDao {
private static Map<String,Book> bookMap = new LinkedHashMap<String, Book>();
private BookDao() {
}
static{
bookMap.put("1", new Book("1","1111","11.0","zqwang","111 ","111111111"));
bookMap.put("2", new Book("2","2222","22.0","zqwang","222 ","222222222"));
bookMap.put("3", new Book("3","3333","33.0","zqwang","333 ","333333333"));
}
public static Map<String,Book> getBooks(){
return bookMap;
}
public static Book getBook(String id){
return bookMap.get(id);
}
}
3.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1.
Map<String,Book> map = BookDao.getBooks();
for(Map.Entry<String , Book> entry : map.entrySet()){
Book book = entry.getValue();
response.getWriter().write("<a href='"+request.getContextPath()+"/servlet/BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br>");
}
response.getWriter().write("<hr>");
//2.
Cookie [] cs = request.getCookies();
Cookie findC = null;
if(cs!=null){
for(Cookie c : cs){
if("last".equals(c.getName())){
findC = c;
}
}
}
if(findC == null){
response.getWriter().write(" !");
}else{
response.getWriter().write(" :<br>");
String[] ids = findC.getValue().split(",");
for(String id : ids){
Book book = BookDao.getBook(id);
response.getWriter().write(book.getName()+"<br>");
}
}
}
4.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1. id, ,
String id = request.getParameter("id");
Book book = BookDao.getBook(id);
if(book==null){
response.getWriter().write(" !");
return;
}else{
response.getWriter().write("<h1> :"+book.getName()+"</h1>");
response.getWriter().write("<h3> :"+book.getAuth()+"</h3>");
response.getWriter().write("<h3> :"+book.getPrice()+"</h3>");
response.getWriter().write("<h3> :"+book.getPublish()+"</h3>");
response.getWriter().write("<h3> :"+book.getDescription()+"</h3>");
}
//2. cookie
// --- 1 --> 1
// 1 --2,1 --> 2,1
// 2,1--3,2,1 --> 3,2,1
// 3,2,1 -- 4,3,2 --> 4,3,2
// 4,3,2 --3,4,2 --> 3,4,2
String ids = "";
Cookie [] cs = request.getCookies();
Cookie findC = null;
if(cs!=null){
for(Cookie c : cs){
if("last".equals(c.getName())){
findC = c;
}
}
}
if(findC == null){
//
ids += book.getId();
}else{
// ,
String [] olds = findC.getValue().split(",");
StringBuffer buffer = new StringBuffer();
buffer.append(book.getId()+",");
for(int i = 0;i<olds.length && buffer.toString().split(",").length<3 ;i++){
String old = olds[i];
if(!old.equals(book.getId())){
buffer.append(old+",");
}
}
ids = buffer.substring(0, buffer.length()-1);
}
Cookie lastC = new Cookie("last",ids);
lastC.setMaxAge(3600*24*30);
lastC.setPath(request.getContextPath());
response.addCookie(lastC);
}
이상은 본고의 전체 내용입니다. 여러분이 Java가 최근에 본 책을 쿠키로 표시하는 방법을 배우는 데 도움이 되기를 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.