HttpServletRequest和HttpServletResponse

HttpServletRequest

这个东西封装了客户端提交来的数据

  1. 获取请求头数据
//得到一个枚举集合  
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String name = (String) headerNames.nextElement();
			String value = request.getHeader(name);
			System.out.println(name+"="+value);
			
		}
  1. 获取客户端提交过来的数据
String name = request.getParameter("name");
		String address = request.getParameter("address");
		System.out.println("name="+name);
		System.out.println("address="+address);
		
		//name=zhangsan&name=lisi&name=wangwu 一个key可以对应多个值。
		Map<String, String[]> map = request.getParameterMap();
		Set<String> keySet = map.keySet();
		Iterator<String> iterator = keySet.iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			System.out.println("key="+key + "--的值总数有:"+map.get(key).length);
			String value = map.get(key)[0];
			String value1 = map.get(key)[1];
			String value2 = map.get(key)[2];
			
			System.out.println(key+" ======= "+ value + "=" + value1 + "="+ value2);
		}
获取中文数据

客户端如果发送的数据带有中文可能出现乱码,解决方案如下:

  • GET方式:
  1. 代码转码
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		System.out.println("username=" + username + ", password=" + password);
		//get请求过来的数据,在URL地址栏上已经编码,Tomcat中的getParameter是以ISO-8859-1解码
		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		System.out.println("username=" + username);
	}
  1. 可以在tomcat里面做设置处理 conf/server.xml 加上URIEncoding=“utf-8”

<Connector connectionTimeout=“20000” port=“8080” protocol=“HTTP/1.1” redirectPort=“8443” URIEncoding=“UTF-8”/>

POST方式:
在这里插入图片描述出现乱码

设置请求体中的文字编码request.setCharacterEncoding(“UTF-8”);
这行设置一定要写在getParameter之前。该方法对get无用,get方法是在地址栏中获得数据,post是在请求体中获得。

HttpServletResponse

负责返回数据到客户端

  • 输出数据到页面
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//字符流
//		resp.getWriter().write("<h1>hello</h1>");
		//字节流
		resp.getOutputStream().write("world".getBytes());
	}
  • 响应的数据有中文,那么可能乱码
  1. 以字符流输出

resp.getWriter().write("<h1>我爱你西安</h1>");

结果:?????
因为这里写出去的数据默认使用 ISO-8859-1编码

解决方案:

@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//指定输出到客户端使用什么形式编码
		resp.setCharacterEncoding("UTF-8");
		//指定浏览器使用什么形式编码来看这份数据
		resp.setHeader("Content-Type", "text/html;charset=utf-8");
		resp.getWriter().write("<h1>我爱你西安</h1>");
	}
  1. 以字节流输出
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setHeader("Content-Type", "text/html;charset=utf-8");
		resp.getOutputStream().write("<h1>我爱你西安</h1>".getBytes("UTF-8"));
	}
  1. 综合解决方案
    只要保证服务器响应给客户端的数据编码和客户端看这份数据使用的编码方式一样就可以了。
    对于字节流字符流都适用的方案是:

resp.setContentType(“text/html; charset=utf-8”);

演练下载资源

在这里插入图片描述第一种:
在这里插入图片描述
在浏览器访问时,鼠标放在 “女孩.jpg” 时会出现下载路径,只需要写上超链接,不需要任何其他的代码操作就可以下载。原因是Tomcat上有一个默认的Servlet——DefaultServlet,专门用来处理放在Tomcat服务器上的静态资源。

第二种:自己写的方式

  1. 英文资源
    index.html:

<a href=“Demo?filename=aa.jpg”>女孩.jpg</a>
点击自写下载后的 女孩.jpg“”

在这里插入图片描述

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String filename = request.getParameter("filename");
		//获取Tomcat里的绝对路径
		String path = getServletContext().getRealPath("download/" + filename);
		//当浏览器接收到这份资源时,以提醒用户下载模式展示,而不是直接显示
		response.setHeader("Content-Disposition", "attachment; filename=" + filename);
		InputStream is = new FileInputStream(path);
		OutputStream os = response.getOutputStream();
		
		int len = 0;
		byte[] buffer = new byte[1024];
		while(-1 != (len = is.read(buffer))) {
			os.write(buffer, 0, len);
		}
		
		os.close();
		is.close();
	}
  1. 中文资源
    针对火狐浏览器的编码形式Base64的工具:
public class DownloadUtil {
	public static String base64EncodeFileName(String filename) {
		BASE64Encoder base64Encoder = new BASE64Encoder();
		try {
			return "=?UTF-8?B?" + new String(base64Encoder.encode(
					filename.getBytes("UTF-8"))) + "?=";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
//获取Tomcat里的绝对路径
String path = getServletContext().getRealPath("download/" + filename);
//获得访问的客户端类型
String clientType = request.getHeader("User-Agent");
if(clientType.contains("Firefox")) {//火狐浏览器
	filename = DownloadUtil.base64EncodeFileName(filename);
}else {//IE或谷歌使用
	filename = URLEncoder.encode(filename, "UTF-8");
}

请求转发和重定向

  • 重定向

之前的写法
response.setStatus(302);
response.setHeader(“Location”, “login_success.html”);
//重定向写法: 重新定位方向 参数即跳转的位置
response.sendRedirect(“login_success.html”);

  1. 地址上显示的是最后的那个资源的路径地址
  2. 请求次数最少有两次, 服务器在第一次请求后,会返回302 以及一个地址, 浏览器在根据这个地址,执行第二次访问。
  3. 可以跳转到任意路径。 不是自己的工程也可以跳。
  4. 效率稍微低一点, 执行两次请求。
  5. 后续的请求,没法使用上一次的request存储的数据,或者 没法使用上一次的request对象,因为这是两次不同的请求。
  • 请求转发
    //请求转发的写法: 参数即跳转的位置

      request.getRequestDispatcher("login_success.html").forward(request, response);
    
  1. 地址上显示的是请求servlet的地址。 返回200 ok
  2. 请求次数只有一次, 因为是服务器内部帮客户端执行了后续的工作。
  3. 只能跳转自己项目的资源路径 。
  4. 效率上稍微高一点,因为只执行一次请求。
  5. 可以使用上一次的request对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值