2014.09.06 周六-response-request(浏览)

本文详细介绍了HTTP Response的中文输出问题及其解决方案,包括编码设置、文件下载及随机图片生成等特性,并探讨了Request的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只是快速浏览了一下,知道个概念,不想操作了。还是上班好玩一下,放假好无聊。

一、resonse

1、输出中文问题:ResponseDemo1.java

  1. 两边默认的都是gb2312,所以可以输出“中国”。
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String data = "中国";
    		OutputStream out = response.getOutputStream();
    		out.write(data.getBytes());
    	}	
  2. 改成,“UTF-8”,输出就变成“涓浗”了。因为一个用的是gb2312,一个用的是UTF-8。
    		String data = "中国";
    		OutputStream out = response.getOutputStream();
    		out.write(data.getBytes("UTF-8"));
    解决:把浏览器的 --> 编码 改为 “UTF-8”,就能正常显示“中国”了。
  3. 不可能每次都去浏览器改码表吧,所以专业的做法是:向浏览器发送数据的时候,告诉它我是utf-8
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String data = "中国";
    		
    		response.setHeader("content-type", "text/html;charset=UTF-8");
    		
    		OutputStream out = response.getOutputStream();
    		out.write(data.getBytes("UTF-8"));
    	}	
    在服务器端,数据是以哪个码表输出的,就要控制浏览器已哪个码表打开。
  4. 使用html语言里面的<meta>标签来控制浏览器行为(模拟http响应头)
    	String data = "中国";
    
    		OutputStream out = response.getOutputStream();
    		out.write("<meta http-equiv='Content-type' content='text/html;charset=UTF-8'>".getBytes());
    		out.write(data.getBytes("UTF-8"));
    问题:在ie里面只显示“中国”,可为什么在谷歌里面却显示“
    <meta http-equiv='Content-type' content='text/html;charset=UTF-8'>涓浗
    ”呢?
  5. 同时发两个头:
    	private void test3(HttpServletResponse response) throws IOException,
    			UnsupportedEncodingException {
    		String data = "中国";
    
    		OutputStream out = response.getOutputStream();
    		out.write("<meta http-equiv='Content-type' content='text/html;charset=gb2312'>"
    				.getBytes());
    		response.setHeader("Content-type", "text/html;charset=UTF-8");
    
    		out.write(data.getBytes("UTF-8"));
    	}
  6. 用字符流输出数据:
    	private void test4(HttpServletResponse response) throws IOException,
    			UnsupportedEncodingException {
    		String data = "中国";
    
    		//通知浏览器以utf-8打开
    		response.setHeader("Content-type", "text/html;charset=UTF-8");
    		
    		//通知response以utf-8输出
    		response.setCharacterEncoding("UTF-8");
    		PrintWriter out = response.getWriter();  //iso8859-1  ??
    		out.write(data);
    	}
    小细节:
    //response.setHeader("Content-type", "text/html;charset=UTF-8");
    		response.setContentType("text/html;charset=UTF-8");
  7. private void test5(HttpServletResponse response) throws IOException,
    			UnsupportedEncodingException {
    		response.getOutputStream().write(1);  //在浏览器看不到1
    		response.getOutputStream().write((1 + "").getBytes());  //这样就看到1了
    	}
2、文件下载
3、response生成随机图片
4、请求重定向
  1. 应用:登录
  2. 特点:浏览器向服务器发送了两次请求;浏览器地址栏会发生改变。
  3. 能不用尽量不用。

二、request

1、常用方法
2、input输入项(html表单<form></form>)























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值