web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse对象。
如果获取客户端请求过来的参数找:HttpServletRequest;
如果要给客户端响应一些信息找:HttpServletResponse;
负责向浏览器发送数据的方法:
getOutputStream();
getWriter()…
负责向浏览器发送响应头的方法:
setCharacterEncoding();
setContentLength();
setContentType()…
1、向浏览器输出消息;
2、下载文件:
- 获得下载的文件路径
- 下载的文件名是?
- 设置想办法让浏览器能够支持下载我们需要的东西
- 获取下载文件的输入流
- 创建缓冲区
- 获取OutputStream对象
- 将FileOutputStream流写入到buffer缓冲区
- 使用OutputStream将缓冲区中的数据输出到客户端
1.步骤一:准备一张图片放入resource目录中
2.步骤二:编写FileServlet类实现图片下载功能
package com.massimo.charpter11;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得要下载文件的路径
String realPath = "D:\WorkSpace\idea2019\javaweb-temp-maven\Response\target\Response-1.0-SNAPSHOT\WEB-INF\classes\1.png";
//下载的文件名是?
String fileName = realPath.substring(realPath.lastIndexOf("\") + 1);
//设置想办法让浏览器能够支持下载我们需要的东西s: "Content -Disposition", s1: " attachment ; filename="+fi1eName );
resp.setHeader("Content-Disposition" , "attachment;filename=" + fileName);
//获取下载文件的输入流
FileInputStream fis = new FileInputStream(realPath);
//创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//获取OutputStream对象
ServletOutputStream os = resp.getOutputStream();
//将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端
while((len = fis.read(buffer)) != -1){
os.write(buffer , 0 , len);
}
fis.close();
os.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注意:文件的下载路径应该在像如下寻找。
3.步骤三:注册Servlet,运行程序
验证码怎么来的?
前端实现:
后端实现:需要用到Java的图片类,生产一个图片;
1.步骤一:编写ImageServlet类,实现验证码功能
package com.massimo.charpter7;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
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;
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//如何让浏览器3秒自动刷新一次
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 , Font.BOLD , 20));
g.drawString(makeNum() , 0 , 20);
//告诉浏览器,这个请求用图片的方式打开
resp.setContentType("image/jpeg");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("expires" , -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;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req , resp);
}
}
2.步骤二:注册Servlet,运行程序
一个web资源收到客户端请求后,会通知客户端去访问另外一个web资源,这个过程叫重定向
常见场景:用户登录。
案例一:简单的response重定向1.导入相关依赖后,编写RedirectServlet实现重定向
package com.massimo.charpter7;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/massimo/image");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.注册Servlet,运行程序
<servlet>
<servlet-name>imageservlet-name>
<servlet-class>com.massimo.charpter7.ImageServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>imageservlet-name>
<url-pattern>/imageurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>redirectservlet-name>
<servlet-class>com.massimo.charpter7.RedirectServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>redirectservlet-name>
<url-pattern>/redurl-pattern>
servlet-mapping>
案例二:重定向用户登录
1.步骤一:导入相关依赖后,编写jsp页面,实现重定向登录
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="${pageContext.request.contextPath}/login">
用户名:<input type="text" name="userName"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
form>
body>
html>
2.步骤二:编写RequestTest类,处理登录页面提交的请求
package com.massimo.charpter7;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RequestTest extends HttpServlet {
@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 + ":" + password);
resp.sendRedirect("/massimo/success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req , resp);
}
}
3.步骤三:编写jsp页面,作为登录成功页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<h1>congratulations on your success!h1>
body>