Response 和 Request
一.Response:Response 代表响应,响应音讯中的状态码、响应头、实体内容都能够由它进行操作
1.Response 输入数据到客户端
response.setContentType("text/html;charset=utf-8") ···解决乱码
response.getOutputStream().write(···);
response.getWriter.wtite(···);
2.Response 设置 content-disposition 头实现文件下载
response.setHeader("Content-Disposition","attachment;filename=1.jpg"/"attachment;filename="+URLEncoder.encode("图片.jpg", "utf-8")···将下载文件名改为中文);
response.setHeader("Content-Disposition",
InputStream in = new FileInputStream(this.getServletContext().getRealPath("1.jpg"));
OutputStream out = response.getOutputStream();
byte[]bs = new byte[1024];
int i = 0;
while((i=in.read(bs))!=-1){out.write(bs,0,i);
}
in.close();
// 将下载后的文件名改为中文
response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("图片.jpg", "utf-8"));
response.setHeader("Content-Disposition",
InputStream in = new FileInputStream(this.getServletContext().getRealPath("1.jpg"));
OutputStream out = response.getOutputStream();
byte[]bs = new byte[1024];
int i = 0;
while((i=in.read(bs))!=-1){out.write(bs,0,i);
}
in.close();
3.Response 设置 refresh 头管制定时刷新
1)指定多少秒后刷新以后页面
response.getWriter().write(new Date().toLocaleString());
response.setHeader("Refresh", "1");
2)实现注册后“注册胜利,3 秒后跳转到主页”的性能
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("祝贺您注册胜利!3 秒后回到主页.....");
response.setHeader("refresh", "3;url=/Day04/index.jsp");
3)在 HTML 能够利用 <meta http-equiv= “” content=””> 标签模仿响应头的性能
<html>
<head>
<meta http-equiv="Refresh" content="3;url=/Day04/index.jsp">
</head>
<body>
祝贺您注册胜利!3 秒后回到主页......
</body>
</html>
4. 实现验证码
实现验证码(https://segmentfault.com/a/11…
二.Request:Request 代表申请对象,其中封装了对申请中具备申请行、申请头、实体内容的操作的办法
1. 获取客户机信息
1)获取客户端申请的残缺 URL
String url = request.getRequestURL().toString();
System.out.println(url);
2)获取客户端申请的资源局部的名称
String uri = request.getRequestURI();
System.out.println(uri);
3)获取申请行中参数局部
String qStr = request.getQueryString();
System.out.println(qSt):
4)获取申请客户端的 ip 地址
String ip = request.getRemoteAddr();
System.out.println(ip);
5)获取客户机的申请形式
String method = request.getMethod();
System.out.println(method);
6)获取以后 web 利用的名称(须要 web 利用名称时用
request.getContextPath() 示意,以防 web 利用名称有变动)
String name = request.getContextPath();
System.out.println(name);
2.