文章目录
HTTP响应消息1. 请求消息: 客户端发送给服务器端的数据2. 响应消息: 服务器端发送给客户端的数据
Response对象功能: 设置响应消息案例: 重定向案例 : 验证码
ServletContext对象1. 概念2. 获取3. 功能1. 获取MIME类型2. 域对象: 共享数据3. 获取文件的真实(服务器)路径
案例: 文件下载1. 文件下载需求:分析步骤问题
HTTP响应消息
1. 请求消息: 客户端发送给服务器端的数据
数据格式:
请求行请求头请求空行请求体
2. 响应消息: 服务器端发送给客户端的数据
数据格式:
响应行
组成: 协议/版本 响应状态码 状态码描述响应状态码: 服务器告诉客户端浏览器本次请求和响应的一个状态
状态码都是3位数字分类
1xx : 服务器接收客户端消息,但没有接受完成,等待一段时间后,发送1xx多状态码2xx: 成功. 代表2003xx: 重定向. 代表302(重定向), 304(访问缓存)4xx: 客户端错误
代表
404: 请求路径没有对应的资源405: 请求方式没有对应的doXxx方法 5xx: 服务器端错误.代表500(服务器内部出现异常) 响应头
格式: 头名称 : 值常见的响应头:
Content-Type: 服务器告诉客户端本次响应体数据格式以及编码格式Content-disposition: 服务器告诉客户端什么格式打开响应体数据
值:
in-line : 默认值,在当前页面内打开attachment;filename=xxx 以附件形式打开响应体,文件下载: 响应空行响应体 : 传输的数据 响应字符串格式
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 101
Date: Wed, 06 Jun 2018 07:08:42 GMT
<html>
<head>
<title>$Title$</title>
</head>
<body>
hello , response
</body>
</html>
Response对象
功能: 设置响应消息
设置响应行
格式: HTTP/1.1 200 ok设置状态码: setStatus(int sc) 设置响应头:setHeader(String name , String value)设置响应体
使用步骤
获取输出流
字符输出流: PrintWriter getWriter()字节输出流: ServletOutputStream getOutputStream() 使用输出流 : 将数据输出到客户端浏览器
案例: 重定向
完成重定向
重定向: 资源跳转的方式代码实现:
package cn
.itcast
.web
.servlet
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.IOException
;
@WebServlet("/responseDemo01")
public class ResponseDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
System
.out
.println("demo1.......");
response
.sendRedirect("/day15/responseDemo02");
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
this.doPost(request
,response
);
}
}
* 重定向的特点: redirect
1. 地址栏发送变化
2. 重定向可以访问其他站点(服务器)的资源
3. 重定向是两次请求,不能使用request对象来共享数据
* 转发的特点: forward
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求, 可以使用request对象来共享数据
* 路径写法:
1. 相对路径
* 如 : ./index.html
* 不以./开头, 以/开头路径
* 规则: 找到当前资源和目标资源之间的相对位置关系
* ./ : 当前目录
* ../: 上一级目录
2. 绝对路径 : 通过绝对路径可以确定唯一资源
* 如: http://localhost/day15/responseDemo2
* 以/开头的路径
* 规则: 判断定义的路径是给是用的?判断请求将来从哪发出
* 给客户端浏览器使用: 需要加虚拟目录(项目的访问路径)
* 建议虚拟目录动态获取: request.getContextPath()
* < a>, <form> 重定向
* 给服务器使用,不需要加虚拟目录
* 转发路径
服务器输出字符数据到浏览器
步骤:
获取字符输出流输出数据 注意:
乱码问题:
PrintWriter pw = response.getWriter();获取的流的默认编码是ISO-8859-1设置该流的默认编码告诉浏览器响应体使用的编码
//简单的形式,设置编码,是在获取流之前设置 response.setContentType(“text/html;charset=utf-8”)
服务器输出字节数据到浏览器验证码
本质: 图片目的: 防止恶意表单注册
案例 : 验证码
package cn
.itcast
.web
.servlet
;
import javax
.imageio
.ImageIO
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.awt
.*
;
import java
.awt
.image
.BufferedImage
;
import java
.io
.IOException
;
import java
.util
.Random
;
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
int width
=100;
int height
= 50;
BufferedImage image
= new BufferedImage(width
, height
, BufferedImage
.TYPE_INT_RGB
);
Graphics g
= image
.getGraphics();
g
.setColor(Color
.PINK
);
g
.fillRect(0,0,width
,height
);
g
.setColor(Color
.BLUE
);
g
.drawRect(0,0,width
-1,height
-1);
String str
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
Random r
= new Random();
for (int i
=1;i
<=4;i
++){
int index
= r
.nextInt(str
.length());
char c
= str
.charAt(index
);
g
.drawString(c
+"",width
/5*i
,height
/2);
}
g
.setColor(Color
.green
);
for (int i
=0;i
<10;i
++){
int x1
= r
.nextInt(width
);
int x2
= r
.nextInt(width
);
int y1
= r
.nextInt(height
);
int y2
= r
.nextInt(height
);
g
.drawLine(x1
,y1
,x2
,y2
);
}
ImageIO
.write(image
,"jpg",response
.getOutputStream());
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
this.doPost(request
,response
);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码
</title>
<script>
window.onload =function () {
var img = document.getElementById("checkCode");
img.onclick =function () {
var date = new Date().getTime();
img.src = "/day15/checkCodeServlet?"+date;
}
var img1 = document.getElementById("change");
img1.onclick =function () {
var date = new Date().getTime();
img1.src = "/day15/checkCodeServlet?"+date;
}
}
</script>
</head>
<body>
<img id="checkCode" src="/day15/checkCodeServlet">
<a id="change" src="/day15/checkCodeServlet" href="">看不起,换一张
</a>
</body>
</html>
ServletContext对象
1. 概念
代表整个web应用,可以和程序的容器(服务器)来通信
2. 获取
通过request对象获取 request.getServletContext();通过HttpServlet获取 this.getServletContext();
3. 功能
1. 获取MIME类型
MIME类型: 在互联网通信过程中定义的一种文件数据类型
格式 大类型/小类型 text/html image/jpeg: 获取:String getMimeType(String file)
2. 域对象: 共享数据
setAttibute(String name , Object value)getAttribute(String name)removeAttribute(String name)
ServletContext对象范围: 所有用户请求的数据
3. 获取文件的真实(服务器)路径
方法: String getRealPath(String path)
package cn
.itcast
.web
.servletcontext
;
import javax
.servlet
.ServletContext
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.IOException
;
@WebServlet("/servletContextDemo05")
public class ServletContextDemo05 extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
ServletContext context
= this.getServletContext();
String b
= context
.getRealPath("/b.txt");
System
.out
.println(b
);
String c
= context
.getRealPath("/WEB-INF/c.txt");
System
.out
.println(c
);
String a
= context
.getRealPath("/WEB-INF/classes/a.txt");
System
.out
.println(a
);
String a1
= context
.getRealPath("src/a.txt");
System
.out
.println(a1
);
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
this.doPost(request
,response
);
}
}
案例: 文件下载
1. 文件下载需求:
页面显示超链接点击超链接后弹出下载提示框完成图片文件下载
分析
超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。不满足需求任何资源都必须弹出下载提示框使用响应头设置资源的打开方式:
content-disposition:attachment;filename=xxx
步骤
定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename定义Servlet
获取文件名称使用字节输入流加载文件进内存指定response的响应头: content-disposition:attachment;filename=xxx将数据写出到response输出流
问题
中文文件问题
解决思路:
获取客户端使用的浏览器版本信息根据不同的版本信息,设置filename的编码方式不同
package cn
.itcast
.web
.download
;
import cn
.itcast
.web
.utils
.DownLoadUtils
;
import javax
.servlet
.ServletContext
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.ServletOutputStream
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.FileInputStream
;
import java
.io
.IOException
;
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
String filename
= request
.getParameter("filename");
ServletContext servletContext
= this.getServletContext();
String realPath
= servletContext
.getRealPath("/img/" + filename
);
FileInputStream fis
= new FileInputStream(realPath
);
String mimeType
= servletContext
.getMimeType(filename
);
response
.setHeader("content-type",mimeType
);
String agent
= request
.getHeader("user-agent");
filename
= DownLoadUtils
.getFileName(agent
,filename
);
response
.setHeader("content-disposition","attachment;filename="+filename
);
ServletOutputStream sos
= response
.getOutputStream();
byte[] buff
= new byte[1024 * 8];
int len
= 0;
while((len
= fis
.read(buff
)) != -1){
sos
.write(buff
,0,len
);
}
fis
.close();
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
this.doPost(request
,response
);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载
</title>
</head>
<body>
<a href="/day15/img/2.jpg">图片
</a>
<a href="/day15/img/1.avi">视频
</a>
<hr>
<a href="/day15/downloadServlet?filename=九尾.jpg">中文图片
</a>
<a href="/day15/downloadServlet?filename=1.avi">视频
</a>
</body>
</html>