ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

大话HTTP协议漫画+图解打造高中生也能学会的编程基础课程

2021-04-14 07:03:05  阅读:214  来源: 互联网

标签:HTTP String 大话 public getWriter book id 图解 response


download:大话HTTP协议漫画+图解打造高中生也能学会的编程基础课程

无论从实际工作还是认知的角度,系统学习HTTP都非常必要,这是很多人刚需,但不论是学校还是市面上的教程很少有既系统全面又贴近实际工作的,这是我们制作此课程的出发点和目的

适合人群
HTTP应用非常广泛,我们的课程适用于包括
研发、运维、测试、产品、项目经理等在内的所有人
甚至是非计算机行业的人

技术储备要求
无技术储备要求
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;
}

}

25
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);
}

}

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()+"
");
}
response.getWriter().write("
");

    //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("您曾经阅读过的书:

");
String[] ids = findC.getValue().split(",");
for(String id : ids){
Book book = BookDao.getBook(id);
response.getWriter().write(book.getName()+"
");
}
}
}

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("
书名:"
+book.getName()+"");
response.getWriter().write("
作者:"
+book.getAuth()+"");
response.getWriter().write("
售价:"
+book.getPrice()+"");
response.getWriter().write("
出版社:"
+book.getPublish()+"");
response.getWriter().write("
描画信息:"
+book.getDescription()+"");
}

    //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);
}

标签:HTTP,String,大话,public,getWriter,book,id,图解,response
来源: https://blog.51cto.com/u_15161311/2704536

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有