JavaWeb——Day05_1

*1.jsp入门

会话!







Jsp原理:

lWeb服务器是如何调用并执行一个jsp页面的?
l
lJsp页面中的html排版标签是如何被发送到客户端的?
l
lJsp页面中的java代码服务器是如何执行的?
l
lWeb服务器在调用jsp时,会给jsp提供一些什么java对象?







html标签翻译成什么了?代码又发生了什么变化?







jsp语法:

lJSP模版元素
lJSP表达式
lJSP脚本片断
lJSP注释
lJSP指令
lJSP标签
lJSP内置对象
l如何查找JSP页面中的错误 

















*2.Cookie入门


servletcontext。

request。

这两个域对于会话产生的数据不能合理保存。






这是客户端技术,每个浏览器都保存一段信息。





Session技术:



session是服务端技术。


先讲Cokkie技术,下午Session。

演示Cookie:

 






改:

Long lastTime = Long.parse(findC.getValue());

response.setContentType("text/html,charset="utf-8"")



加一个第一次访问的判断cookie是否为空。









用另一个浏览器看还是能看到的。







Cookie显示用户上次浏览的商品。


*3.Cookie案例_最后看过的书





  





*4.Cookie案例_最后看过的书2

改造下:让它显示最进看过的几本书。





代码:

Book.java

package com.itheima.domain;

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

}
BookDao.java

package com.itheima.cookie;

import java.util.LinkedHashMap;
import java.util.Map;

import com.itheima.domain.Book;

public class BookDao {
	private static Map<String,Book> bookMap = new LinkedHashMap<String, Book>();
	private BookDao() {
	}
	static{
		bookMap.put("1", new Book("1","三国演义","99.0","朴乾","黑马出版社","一群男人纠结不清的故事...."));
		bookMap.put("2", new Book("2","西游记","10.0","曹睿","传智出版社","一个和尚一个猴子一头猪和一个秃子去西天的故事..."));
		bookMap.put("3", new Book("3","水浒传","2.0","奥巴马","人民教育出版社","105个男人和3个女人闯荡江湖的故事"));
		bookMap.put("4", new Book("4","金瓶梅","200.0","哈利波特","科技出版社","混乱不堪的故事..."));
	}
	
	public static Map<String,Book> getBooks(){
		return bookMap;
	}
	
	public static Book getBook(String id){
		return bookMap.get(id);
	}
}

BookInfoServlet.java

package com.itheima.cookie;

import java.io.IOException;

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 com.itheima.domain.Book;

public class BookInfoServlet extends HttpServlet {

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

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

}
BookServlet.java

package com.itheima.cookie;

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

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 com.itheima.domain.Book;

public class BookListServlet extends HttpServlet {

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

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

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值