Day 41 学习分享 - Cookie和Session

1. 重定向(Redirect)和转发(forward)区别:
相同点:
  都可以实现请求的跳转


不同点;

  > 语法不同:
  重定向: response.sendRedirect("main.html");
  转发:   request.getRequestDispatcher("main.html").forward(req,resp);

  > 请求次数不同:
  重定向一共浏览器发送了2次请求
  转发是服务器内部跳转,只发送了1次请求

  > 是否可以共享Request作用域中值:
  重定向因为发送了2次去请求,所以Request中存储的值,不能跨请求共享!
  转发因为是服务器内部跳转,使用的是同一个request对象,所以可以共享request中的值!

  存值转发!
2. 今日相关代码:
Cookie 以及 Session 都是键值对的形式储存数据的
Cookie c = new Cookie(String name, Object obj);
	Cookie构造方法, 创建一个新的Cookie对象

c.setMaxAge(int age);
 	设置cookie对象的存活时间, 单位为秒, 如果设置为-1代表消除当前cookie对象
 
c.setPath(String path);
	设置cookie对象的作用域, 例如:"/view" 代表view文件夹下都可使用该cookie
	"/" 代表全区域
	
HttpServletResponse response:
	response.addCookie(Cookie c);
		将cookie对象传入response中使其能在其他页面被获取到
		
HttpServletRequest request:
	Cookies[] cookies = request.getCookies();
		从request中获取到所有的cookies数组
	
String cookie.getName();
	获取到当前cookie的Key值
	
Object cookie.getValue();
	获取到当前cookie的Value值
 	
HttpSession session = request.getSession();
 	创建一个HttpSession对象session
 	
session.setMaxInactiveInterval(int age);
 	创建session对象的存活时间, 单位为秒
 	
session.setAttribute(String key, Object value);
 	设置session中要进行传递的参数的key,value;
 	
session.getAttribute(String key);
 	通过key值获取session中传递的参数
        

web.xml中配置HttpSession对象存活时间的方法为:
	<session-config>age</session-config> age的单位为分钟!
// 验证码生成器
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int width=80;
		int height=25;
		//创建一个画布对象
		BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//通过xxx得到一个画笔
        Graphics g = bi.getGraphics();
		//设置画笔颜色为白色
        g.setColor(Color.WHITE);
		//画一个实心矩形
        g.fillRect(0, 0, width, height);
		g.setColor(Color.BLACK);
		//画一个空心矩形,-1达到一个框框的显示效果
        g.drawRect(0, 0, width-1, height-1);
		//62个大小字母以及数字的字符数组
		char[] array = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
				'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
		Random r=new Random();
		StringBuffer sb=new StringBuffer();
        //画干扰线
		for(int i=0;i<6;i++){
            //获取三个随机色值
			int red=r.nextInt(255);
			int green=r.nextInt(255);
			int blue=r.nextInt(255);
            //设置颜色
			g.setColor(new Color(red,green,blue));
            //起始坐标
			int x1=r.nextInt(width);
			int y1=r.nextInt(height);
            //干扰线的终止坐标
			int x2=r.nextInt(width);
			int y2=r.nextInt(height);
			g.drawLine(x1, y1, x2, y2);
		}
		//绘制四个随机数
		for(int i=1;i<=4;i++){
			int index=r.nextInt(62);
			String str = array[index]+"";
			sb.append(str);
            /获取三个随机色值
			int red=r.nextInt(255);
			int green=r.nextInt(255);
			int blue=r.nextInt(255);/
            //设置颜色
			g.setColor(new Color(red,green,blue));
            //设置字体黑体,加粗,18号
			g.setFont(new Font("黑体", Font.BOLD, 18));
            //画验证码中的内容
			g.drawString(str, i*16, 18);
		}
		HttpSession session = request.getSession();
        //将验证码存入到session中
		session.setAttribute("uCode", sb.toString());
		//设置输出内容的类型是图片
		response.setContentType("image/jpeg");
        //获得一个浏览器字节输出流
		ServletOutputStream out = response.getOutputStream();
        //输出这个画布
		ImageIO.write(bi, "jpeg", out);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值