请求转发:一种在服务器内部的资源跳转方式
使用步骤
1.通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)参数是转发资源的路径
2.使用Requestispatcher对象进行转发
特点:浏览器地址栏路径不会发生变化,只能转发当前服务器内部资源,转发是一次请求
共享数据
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
方法:
- void setAttribute(String name,Object obj):存储数据
- Object getAttiude(Stirng name):通过键获取值
- void removeAttribute(String name):通过键移除键值对
package com.Data.HttpServlet;
import javax.servlet.RequestDispatcher;
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("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使用request对象的方法setAttribute将数据存储到request域中
request.setAttribute("msg","Hello");
System.out.println("RequestDemo1被访问了。。。。");
//转发到requestDemo2
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo2");
requestDispatcher.forward(request,response);
}
}
package com.Data.HttpServlet;
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("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问共享数据,通过键获取值
System.out.println(request.getAttribute("msg"));
System.out.println("RequestDemo2被访问了。。。");
}
}
RequestDemo1被访问了。。。。
Hello
RequestDemo2被访问了。。。
获取ServletContext:
ServletContext getServletContext()
HTTP响应消息
服务器端发送给客户端的数据
五种数据格式
1.响应行
1:组成:协议/版本 响应状态吗 状态码描述
2.响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态,状态码都是3位数字有5种状态码
- 1xx :服务器接收客户端消息,但没有接收完成,等待一段时间后,发送1xx多状态码
- 2xx :成功 。代表数字:200
- 3xx :重定向。代表:302(重定向),304(访问缓存)
- 4xx :客户端错误。代表(404)请求路径没有对应的资源,405(请求方式没有对应的doxxx方法)
- 5xx : 服务端错误。代表:500(服务器内部出现异常,比如代码有错)
2.响应头
格式:头名称 :值
常见的响应头
Content-Type:服务器告诉客户端本次响应体数据格式以继编码格式
Content-dispostition:服务器告诉客户端以什么格式打开响应体数据
3.响应空行
4响应体:传输的数据
重定向:资源跳转方式
package com.data;
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("/Demo1")
public class ResponseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo1....");
//完成重定向
//设置状态码为302
//response.setStatus(302);
//设置响应头,头的名称,头的值
//response.setHeader("location","/Demo2");
//重定向可以访问其他站点资源
//response.sendRedirect("https://www.baidu.com");//访问了百度
//重定向
response.sendRedirect("/Demo2");
//设置共享数据
request.setAttribute("msg","hello");
}
}
package com.data;
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("/Demo2")
public class ResponseServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向可以访问其他站点的资源
System.out.println("demo2.....");
//重定向不能访问共享数据
Object o1 = request.getAttribute("msg");
System.out.println(o1);
}
}
输出结果
demo1....
demo2.....
null
重定向的特点:redirect
- 地址栏发生变化
- 重定向可以访问其它站点服务器的资源
- 重定向是两次请求,,不拿使用request对象来共享数据
路径写法
相对路径:通过相对路径不可以确定唯一资源
绝对路径: 通过绝对路径可以确定唯一资源,给客户端浏览器使用需要加虚拟目录,给服务器使用,不需要加虚拟目录
服务器输出字符数据到浏览器
package com.data;
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("/Demo3")
public class ResponseServlet3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使用响应头,告诉浏览器我使用的编码
//浏览器收到响应,就会按照指定字符集解码
response.setContentType("text/html;charset=utf-8");
//response.setHeader("content-type","text/html;charset=utf-8");
//向客户端输出数据,先要获取字符输出流
PrintWriter writer = response.getWriter();
writer.write("<h1>你好,world</h1>");
}
}
服务器输出字节数据到浏览器
package com.data;
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;
import java.io.PrintWriter;
@WebServlet("/Demo3")
public class ResponseServlet3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write("你好,客户端".getBytes("utf-8"));
}
}
验证码
package com.data;
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("/Demo4")
public class CheckCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 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="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 写验证码
Random ran = new Random();
for(int i = 1;i<=4;i++){
int index = ran.nextInt(str.length());
char ch = str.charAt(index);
g.drawString(ch+"",width/5*i,height/2);
}
g.setColor(Color.GREEN);
for (int i = 0 ; i< 10 ;i++){
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//将图片输出到页面上
ImageIO.write(image,"jpg",resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
// 分析:
// 点击超链接或者图片,需要换一张,
// 给图片和超链接绑定单击事件
window.onload=function () {
//获取图片对象
var img=document.getElementById("change");
//绑定单击事件
img.onclick=function () {
var date =new Date().getTime();
img.src="/day14/Demo4"+date;
}
}
</script>
</head>
<body>
<img id="checkCode" src="/day14/Demo4" />
<a id="change" href="">看不清?换一张</a>
</body>
</html>
ServletContext对象:
1.概念:代表整个web应用,可以和程序的容器来通信
获取方式:
1.request.getServletContext();
2.this.getServletContext();
功能:获取MIME类型(在互联网通信过程种定义的一种文件数据类型,不如text/html,image/jpeg)
package com.data;
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("/Demo5")
public class ServletDemo5 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取MIME类型
ServletContext context=this.getServletContext();
//定义文件名称
String filename = "a.jpg";
//获取MIME类型
String mimeType = context.getMimeType(filename);
System.out.println(mimeType);
}
}
//image/jpeg
功能:域对象,共享数据
域对象的方法
setArrtibute(String name,Object value)
getAttribute(String name)
removeAttribute(String name)
功能:获取文件服务器路径
文件在项目目录下,可以直接文件名获取路径
在WEB-INF目录下,需要加上WEB-INF才可以获取
src目录下的所有文件将来都存放在/WEB-INF/classes目录下
package com.data;
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("/Demo5")
public class ServletDemo5 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取web目录下的资源访问
ServletContext context = this.getServletContext();
String realPath = context.getRealPath("/b.txt");//b.txt在项目目录下,可以直接文件名获取路径
System.out.println(realPath);
//获取WEB-INF目录下的资源访问
String realPath1 = context.getRealPath("/WEB-INF/c.txt");//c.txt在WEB-INF目录下,需要加上WEB-INF才可以获取到路径
System.out.println(realPath1);
//获取src目录下的资源能路径
String realPath2 = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的所有文件将来都存放在/WEB-INF/classes目录下
System.out.println(realPath2);
}
}