本人个人学习笔记 未经授权不允许转发
JavaWeb笔记(六)---- Response and Request
目录
1. HttpServletResponse -下载
2. HttpServletResponse - 随机验证码
3. HttpServletRequest - 重定向
4. HttpServletRequest - 请求转发
Web服务器接收到客户端的http请求, 针对这个请求,分别创建一个代表请求的HttpServletRequest和一个代表响应的HttpServletResponse
- 如果要获取客户端请求过来的一些参数:找HttpServletRequest
- 如果要给客户端一些信息:找HttpServletResponse
1. HttpServletResponse-下载
常见应用
- 向浏览器输出信息
- 下载文件
1. 获取下载文件的路径
2. 下载的文件名
3. 让浏览器支持下载
4. 获取下载文件的输入流
5. 创建缓冲区
6. 获取outputStream
7. 将FileOutputStream写入到buff缓冲流
8. 使用outputStream写入到客户端
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取下载路径
String realPath = "/Users/zhangjuntao/IdeaProjects/javaweb-01-maven/javaweblesson/javaweb-03-HTTPResponse/src/main/resources/45.jpg";
//2.获取文件名
String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
//3.让浏览器支持下载`
resp.setHeader("Content-Disposition","atttachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//4.获取下载文件流
FileInputStream in = new FileInputStream(realPath);
//5.设置缓冲区
int len =0;
byte[] buffer = new byte[1024];
//6.获取outputStram对象
ServletOutputStream out = resp.getOutputStream();
//7.将FileOutputStream写入缓冲区,使用outputStream将缓冲区数据输出到客户端
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
out.close();
in.close();
}
2. HttpServletResponse - 随机验证码
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//自动刷新
resp.setHeader("refresh","3");
//
BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,80,20);
g.setColor(Color.BLUE);
g.setFont(new Font(null,0,20));
g.drawString(makeNum(),0,20);
//告诉浏览器这个请求用图片打开
resp.setContentType("image/jpeg");
//让浏览器不缓存
resp.setDateHeader("exprise",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragma","no-cache");
//把图片写给浏览器
ImageIO.write(image,"jpg",resp.getOutputStream());
}
private String makeNum(){
Random random = new Random();
String num = random.nextInt(9999999)+"";
StringBuffer sb = new StringBuffer();
for (int i=0; i<7-num.length(); i++){
sb.append("0");
}
num = sb.toString()+num;
return num;
}
3. HttpServletRequest - 重定向
相同点:
页面都会实现跳转
重定向: B一个web资源收到客户端A的请求后,B会通知A客户端去访问另一个web资源C。重定向是两次请求。重定向是客户端行为。重定向之后地址栏上的地址会发生变化,变化成第二次请求的地址。重定向时的网址可以是任何网址。
请求转发: 转发是一次请求。转发是服务器行为。转发之后地址栏上的地址不会变化,还是第一次请求的地址。转发的网址必须是本站点的网址
在index.jsp中创建一张表单
<html>
<body>
<h2>Hello World!</h2>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名: <input type="text" name="username"> <br>
密码: <input type="text" name="password"> <br>
<input type="submit">
</form>
</body>
</html>
创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Success</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
创建Request类
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("enterig");
String username = req.getParameter("username");
String pwd = req.getParameter("password");
System.out.println(username+":"+pwd);
resp.sendRedirect(req.getContextPath()+"/success.jsp");
}
4. HttpServletRequest - 请求转发
在index.jsp创建表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<div>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
爱好:
<input type="checkbox" name="hobby" value="女孩">女孩
<input type="checkbox" name="hobby" value="电影">电影
<input type="checkbox" name="hobby" value="游戏">游戏
<br>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Success</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
创建LoginServlet类
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String pwd = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
req.getRequestDispatcher("/success.jsp").forward(req,resp);
//请求转发前面不需要加路径 重定向才需要
System.out.println(username);
System.out.println(pwd);
System.out.println(Arrays.toString(hobbies));
}