day02、HttpServletResponse+Cookie
1、HttpServletResponse
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.要获取下载文件的路径
String realPath = "C:\\Users\\50593\\IdeaProjects\\JavaWeb-02-Servlet\\Response1\\src\\main\\resources\\1.jpg";
System.out.println("下载文件的路径"+realPath);
//2.下载的文件名
String filename = realPath.substring(realPath.lastIndexOf("\\") + 1);
//3.设置想办法让浏览器能够支持(Content-Disposition)我们下载我们需要的东西,
//中文文件名URLEncoder.encode编码,例如URLEncoder.encode(filename,"UTF-8")
resp.setHeader("Content-Disposition","attachment;filename="+filename);
//4.获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
//5.创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//6.获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
//7.将FileOutputStream流写入到buffer缓冲区
//8.使用OutputStream将缓冲区中的数据输出到客户端
while((len=in.read(buffer))>0)
{
out.write(buffer,0,len);
}
in.close();
out.close();
}
String filename = realPath.substring(realPath.lastIndexOf("\\") + 1);
//一个很取巧的方式获取到了文件的文件名。 substring() 方法返回字符串的子字符串。
// lastIndexOf(String str): 返回指定子字符串在此字符串中最右边出现处的位置然后再+1就返回了文件名
IO再复习
//4.获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
//5.创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//6.获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
//7.将FileOutputStream流写入到buffer缓冲区
//8.使用OutputStream将缓冲区中的数据输出到客户端
while((len=in.read(buffer))>0)
{
out.write(buffer,0,len);
}
in.close();
out.close();
先把文件写入到流中
FileInputStream in = new FileInputStream(realPath);
创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
然后服务器端创建一个输出流用于输出到客户端中
ServletOutputStream out = resp.getOutputStream();
将文件流写入到缓冲区中 然后再从缓冲区用输出流输出
while((len=in.read(buffer))>0)
{
out.write(buffer,0,len);
}
in.close();
out.close();
HttpSevletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息;
2.HttpServletRequest
1、获取参数 请求转发
req.getParameter(String s) String
//获取请求过来的参数
req.getParameterValues(String s) String[]
//获取请求过来的字符串可以用来获取复选框的内容,返回一个字符串数组
Arrays.toString(hobbies)
//
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
System.out.println("---------------------------");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(hobbies));
System.out.println("---------------------------");
//通过请求转发
//这里的 "/" 代表当前的web应用
req.getRequestDispatcher("/success.jsp").forward(req,resp);//转发
// resp.SendRediction("success.jsp");//重定向
}
面试题:请你聊聊重定向和转发的区别?
相同点:
- 页面都会实现跳转
不同点:
- 请求转发的时候,url不会变化 编号307
- 重定向时候,URL地址会发生变化 编号302
2、Cookie和Session技术
1、Cookie
心得: cookie就像一个信件 每个人都能拿到 信中的内容可能不同 班长只认信而不用在意信的内容
客户端:同学 服务器端:班长
public class CookieDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器告诉你访问的时间,把这个时间封装成一个信件,下次再来带上信件,我就知道你来了
//解决中文乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组 说明cookie存在多个
//判断Cookie是否存在
if(cookies!=null){
//如果存在怎么办 则取出cookie的内容 是一个数组所以用循环取出
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字 如果一样则获取cookie中的值
if (cookie.getName().equals("lastLoginTime")){
out.write("你上次的访问时间");
//获取cookie中的值
//cookie.getValue();获取cookie中的值 返回的是String类型 但是我们需要的是日期类型
long lastLoginTime = Long.parseLong(cookie.getValue());
//把String转换成了long类型
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else{
out.write("这是您第一次访问本站");
}
//服务器给客户端响应一个cookie:
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
//获取本地时间作为lastLoginTime cookie的值currentTimeMillis返回的是long所以加上一个""
//cookie.setMaxAge(24*60*60);//设置cookie过期时间 一天
resp.addCookie(cookie);
}
cookieDemo流程:
1.服务器端先获取客户端的cookie
//Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组 说明cookie存在多个
2.然后判断这个cookie是否有值
if(cookies!=null){
}else{
}
2.1如果是没有值则直接给网页提示 “你是第一次访问本站”
else{
out.write("这是您第一次访问本站");
}
2.1然后给客户端创建一个cookie并传回去
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
//获取本地时间作为lastLoginTime cookie的值currentTimeMillis返回的是long所以加上一个""
//cookie.setMaxAge(24*60*60);//设置cookie过期时间 一天
resp.addCookie(cookie);//传给客户端
2.2、如果有值则把这个cookies[]数组遍历把cookie取出来一一对比
if(cookies!=null){
//如果存在怎么办 则取出cookie的内容 是一个数组所以用循环取出
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字 如果一样则获取cookie中的值
if (cookie.getName().equals("lastLoginTime")){
out.write("你上次的访问时间");
//获取cookie中的值
//cookie.getValue();获取cookie中的值 返回的是String类型 但是我们需要的是日期类型
long lastLoginTime = Long.parseLong(cookie.getValue());
//把String转换成了long类型
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
//获取本地时间作为lastLoginTime cookie的值currentTimeMillis返回的是long所以加上一个""
//cookie.setMaxAge(24*60*60);//设置cookie过期时间 一天
resp.addCookie(cookie);//传给客户端