功能 设置响应消息
1. 设置响应行
1. 格式:HTTP/1.1 200 ok
2. 设置状态码: setStatic(int sc)
2. 设置相应头
setHeader(String name,String value)
3. 设置响应体
使用步骤:
1. 获取输出流
* 字符输出流:PrintWriter getWriter()
* 字节输出流: ServletOutputStream getOutStream()
2. 使用输出流,将数据输出到客户端浏览器
案例 完成重定向 资源跳转的方式。
代码实现: 这是第一个访问的Servlet (/rd1) 这是第二个Servlet (/rd2) 重定向来的 这是运行结果:
特点 转发特点:foward
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求,可以使用request对象来共享数据
重定向特点:redirect
1. 转发地址栏路径发生变化
2. 转发可以访问其他站点(服务器)的资源
3. 转发是两次请求,不能使用request来共享数据
路径的写法
路径的分类
相对路径
不能确定唯一资源
如:./index.html
规则:找到当前资源和目标资源的相对位置关系
./:当前目录
../:后退一级目录
绝对路径
可以确定唯一的资源
*如:http://localhost:8080/3_response_war_exploded/rd2
*以/开头:/3_response_war_exploded/rd2
规则:判断路径给谁用,判断请求从哪儿发出
*给客户端使用:需要加虚拟目录(项目的访问路径)
*建议虚拟目录动态获取:request.getContextPath()
*<a>,<form> 重定向...
*给服务器使用,不需要加虚拟目录
* 转发路径
服务器输出字符数据到浏览器 步骤
获取字符输出流
输出数据
注意:
乱码问题:
1. PrintWriter pw = response.getWriter();
2. 设置该流的默认编码
3. 告诉浏览器,响应体用的编码
1 2 response.setContentType("text/html;charset=UTF-8" );
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.web.servlet;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 (urlPatterns = "/rd3" )public class ResponseDemo3 extends HttpServlet { @Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8" ); PrintWriter pw = response.getWriter(); pw.write("<h1>hello response<h1>" ); pw.write("你好 response" ); } @Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request, response); } }
服务器输出字节数据到浏览器 步骤:
1. 获取字节输出流
2. 输出数据
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.web.servlet;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 (urlPatterns = "/rd4" )public class ResponseDemo4 extends HttpServlet { @Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8" ); ServletOutputStream sos = response.getOutputStream(); sos.write("hello" .getBytes()); } @Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request, response); } }
验证码 1. 本质:图片
2. 目的:防止恶意表单注册
注:以后写的话 ,几乎都是在网上找美观的代码。自己改!!!
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 package com.web.servlet;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.util.*;import java.awt.image.BufferedImage;import java.io.IOException;@WebServlet (urlPatterns = "/rd5" )public class ResponseDemo5 extends HttpServlet { @Override protected void doPost (HttpServletRequest request, HttpServletResponse response) 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 <5 ; 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" ,response.getOutputStream()); } @Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request, response); } }
HTML页面代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > Title</title > </head > <body > <img id ="checkCode" src ="/3_response_war_exploded/rd5" /> <a id ="change" herf ="" > 看不清,换一张?</a > </body > <script > /* 分析: 点击超链接或者图片,需要换一张 1. 给超链接或者图片绑定单击事件 2.重新设置图片的SRC属性值 */ window .onload = function ( ) { var img = document .getElementById("checkCode" ); img.onclick=function () { var date = new Date ().getTime(); img.src = "/3_response_war_exploded/rd5?" +date; } var img2 = document .getElementById("change" ); img2.onclick = function () { var date = new Date ().getTime(); img.src = "/3_response_war_exploded/rd5?" +date; } } </script > </html >
<
HTTP协议之ServletContent对象
速解HTTP协议之响应消息
>