##BeanUtils工具类
BeanUtils工具类,简化数据封装
*用于封装JavaBean的
1.JavaBean:标准的Java类
1.类必须被public修饰
2.必须提供空参的构造器
3.成员变量必须使用private修饰
4.提供公共setter和getter方法
功能:封装数据
2.概念:
成员变量:
属性:setter和getter方法截取后的产物
例如:getUsername() --> Username -->username
第一个红圈里的就是成员变量,第二个红圈里的就是属性
3.方法:
1.setProperty(封装的Bean,属性名,添加的数据)
2.getProperty(封装的Bean,属性名)
3.poplute(Object obj,Map map):将map集合的键值对信息,封装到对应的 JavaBean对象中
@Test
public void BeanUtils(){
User user = new User();
try {
BeanUtils.setProperty(user,"username","zhangsan");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(user);
String password = null;
try {
password = BeanUtils.getProperty(user, "username");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
System.out.println(password);
}
## HTTP协议
1.请求消息:客户端发送给服务器端的数据
*数据格式:
1.请求行
2.请求头
3.请求空行
4.请求体
2.响应消息:服务器端发送给客户端的数据
*数据格式:
1.响应行
1.组成:协议/版本 响应状态码 状态码描述
2.响应状态码:服务器告诉客户端浏览器本次请求个响应的一个状态
1.状态码都是3位数字
2.分类:
1. 1xx:服务器就收客户端消息,但没有接受完成,等待一段时间后,发送1xx多状态码
2. 2xx: 成功。代表200
3. 3xx:重定向。代表:302(重定向),304(访问缓存)
4. 4xx:客户端错误
*404(请求路径没有对应的资源)
*405:请求方式没有对应的doxxx方法
5. 5xx:服务器端错误。 代表:500(服务器内部出现异常)
具体状态码可查看:https://www.runoob.com/http/http-status-codes.html
2.响应头
1.格式:头名称:值
2.常见的响应头:
1.Content - Type:服务器告诉客户端本次响应体数据格式以及编码格式
2.Content - disposition:服务器告诉客户端以什么格式打开响应体数据
*值:1.in - line:默认值,在当前页面内打开
2.attachment;filename=xxx:以附件形式代开响应体。文件下载
3、响应空行
4、响应体 :传输的数据
##Reponse对象
*功能:设置响应消息
1、设置响应行
1.格式:HTTP/1.1 200 ok
2.设置状态码:setStatus(int sc)
2.设置响应头: setHeader(“loction”,虚拟路径+重定向的路径)
3.设置响应体:
*使用步骤:
1.获取输出流
*字符输出流:PrintWriter getWriter()
*字节输出流:ServletOutputStream getOutputStream
2.使用输出流,将数据输出到客户端浏览器
##案例
1.完成重定向:
/*//1.设置状态码
response.setStatus(302);
//2.设置响应头
response.setHeader("location","/lzy/responseDemo2");*/
//简单的重定向方法
response.sendRedirect("/lzy/responseDemo2");
System.out.println("demo11111............");
*重定向的特点: redirect
1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求。不能是用request对象来共享数据
*转发的特点: forward
1.转发地址栏路径不变
2.转发只能访问当前服务器下的资源
3.转发是一次请求,可以使用request对象来共享数据
*路径写法:
1、路径分类
1.相对路径:通过相对路径不可以确定唯一资源
*如:./index.html
*不以/开头,以.为开头路径
*规则:找到当前资源和目标资源之间的相对位置关系
* ./:当前目录
* ../:后退一级目录
2.绝对路径:通过绝对路径可以确定唯一资源
*如:http://localhost/day/responseDemo2
*以/为开头路径
*规则:判断定义的路径是给谁用的?判断请求将来从哪触发
*给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
*建议虚拟目录动态获取:request.getContextPath()
*给服务器使用:不需要加虚拟目录
*转发不需要加虚拟目录,别的应该加虚拟目录
2.服务器输出字符数据到浏览器
*步骤:
1.获取字符输出流
2.输出数据
*注意:
*乱码问题:
1.PrintWriter pw = response.getWriter(); 获取的流的默认编码是ISO-8859-1
2.设置该留的默认编码
3.告诉浏览器响应体使用的编码
package Study_Response;
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;
import java.io.PrintWriter;
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setCharacterEncoding("UTF-8");
//告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码
response.setHeader("content-type","text/html;charset=utf-8");
//简单的形式,设置编码,是在获取流之前设置
response.setContentType("text/html;charset=utf-8");
//1.获取字符输入流
PrintWriter writer = response.getWriter();
//2.输出数据
writer.write("<h1>hello response</h1>");
writer.write("你好,response!!!!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
3.服务器输出字节数据到浏览器
*步骤:
1.获取字节输出流
2.输出数据
package Study_Response;
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.IOException;
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1.获取字节输出流
ServletOutputStream outputStream = response.getOutputStream();
//2.输出数据
outputStream.write("你好".getBytes("utf-8"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
4.验证码
package Study_Practice;
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 height = 50;
int width = 100;
//1.创建对象,在内存中存储照片
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2.美化图片
//2.1填充背景颜色
Graphics g = image.getGraphics();
g.setColor(Color.RED);//设置画笔颜色
g.fillRect(0,0,width,height);
//2.2画边框
g.setColor(Color.blue);
g.drawRect(0,0,width-1,height-1);
//2.3写验证码
String str ="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789" ;
//生成随机角标
Random r = new Random();
for (int i = 0; i <4 ; i++) {
int index = r.nextInt(str.length());
char c = str.charAt(index);
g.drawString(c+"",width/5*i,height/2);
}
//2.3华干扰线
g.setColor(Color.GREEN);
//生成随机坐标点
for (int i = 0; i <5 ; i++) {
int x1 = r.nextInt(width);
int y1 = r.nextInt(height);
int x2 = r.nextInt(width);
int y2 = r.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//3.将图片输出到页面上
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
##ServletContext对象
1.概念:代表整个web应用,可以和程序的容器(服务器)来通信
2.获取:
1.通过request对象获取
request.getServletContext();
2.通过HttpServlet获取
this.getServletContext();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.通过request获取
ServletContext re = request.getServletContext();
System.out.println(re);
//2.通过HttpServlet获取
ServletContext ht = this.getServletContext();
System.out.println(ht);
System.out.println(re == ht);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
3.功能:
1.获取MIME类型
*MIME类型:在互联网通信过程中定义的一种文件数据类型
*格式:大类型/小类型 text/html image/jpeg
*获取:String getMimeType(String file)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过HTTPServlet获取
ServletContext context = this.getServletContext();
//3.定义文件名称
String file = "a.jpg";
//4.获取MIME类型
String mimeType = context.getMimeType(file);
System.out.println(mimeType);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
2.域对象:共享数据
1.setAttribute(String name, Object value)
2.getAttribute(String name)
3.removeAttribute(String name)
*ServletContext对象范围:所有用户所有请求的数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取
ServletContext context = this.getServletContext();
context.setAttribute("msg","hhhhhhhhhhhhhhhhh");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
Object msg = servletContext.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
3.获取文件的真实(服务器)路径
1.方法:String getRealPath(String path)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取
ServletContext context = this.getServletContext();
//获取文件的服务器路径
String realPath = context.getRealPath("/b.txt"); //web目录下资源访问
System.out.println(realPath);
String realPath1 = context.getRealPath("/WEB-INF/b.txt"); //WEB-INF目录下的资源访问
System.out.println(realPath1);
String realPath2 = context.getRealPath("/WEB-INF/classes/c.txt"); //src目录下的资源访问
System.out.println(realPath2);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
文件下载案列 :
*文件下载需求:
1.页面显示超连接
2.点击超连接后弹出下载提示框
3.完成图片文件下载
*分析:
1.超连接指向的资源如果能够被浏览器解析,则在浏览器中展示 ,如果不能解析,则弹出下载提示框。不满足需求
2.任何资源都必须弹出下载提示框
3.使用响应头设置资源的打开方式:
*content-disposition:attachment;filename=xxx
*步骤:
1.定义页面,编辑超连接href属性,指向Servlet,传递资源名称filename
2.定义Servlet
1.获取文件名称
2.使用字节输入流加载文件进内存
3.指定response的响应头:content-disposition:attachment;filename=xxx
4.将数据写到response输出流
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求参数,文件名称
String filename = request.getParameter("filename");
//2.使用字节输入流加载文件进内存
//2.1找到文件服务器路径
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/images/" + filename);
//2.2使用字节流关联
FileInputStream fis = new FileInputStream(realPath);
//3.设置response的响应头
//3.1设置响应头类型:content-type
String mimeType = servletContext.getMimeType(filename); //获取文件的mime类型
response.setHeader("content-type",mimeType);
//3.2设置响应头打开方式:content-disposition
response.setHeader("content-disposition","attachment;filename"+filename);
///4.将输入流的数据写出到输入流中
ServletOutputStream sos = response.getOutputStream();
byte[] buff = new byte[1024*8];
int len = 0;
while((len = fis.read(buff)) != -1 ){
sos.write(buff,0,len);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}