美文网首页JavaWeb
JavaWeb——response

JavaWeb——response

作者: 定格r | 来源:发表于2019-05-28 16:48 被阅读0次

功能:设置响应消息

1.设置响应行

  • 格式:
HTTP/1.1  200 OK 
  • 设置状态码:
setStatus(int sc)

2.设置响应头

setHeader(String name,String value)

3.设置响应体

使用步骤:

1、获取输出流

  • 字符输出流:PrinWriter getWriter()
  • 字节输出流:ServletOutstream getOutputstream()

2、使用输出流,将数据输出到客户端浏览器

4、案例

4.1 、完成重定向

  • 重定向:资源跳转的方式
  • 代码实现:
       //访问 Response1 会自动跳转到 Response2
        //1.设置状态码为 302
       response.setStatus(302);
        //2.设置响应头
       response.setHeader("location","/Response2");
       //简单的重定向方法
        response.sendRedirect("/Response2");

forward 和 redirect 区别

  • 重定向的特点

1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求,不能使用 request 对象来共享数据

  • 转发的特点

1.转发地址路径不变
2.转发只能访问当前服务器下的资源
3.转发是一次请求,可以使用 request 对象来共享数据

  • 路径写法
    路径分类

1.相对路径:通过相对路径不可以确定唯一资源

1> 如:./index.html
不以 / 开头,以 . 开头路径
2> 规则:找到当前资源与目标资源之间的相对位置关系
./:当前目录
../:后退一级目录

2.绝对路径:通过绝对路径可以确定唯一资源

1> 如:http://localhost:8080/Response2
以 /开头的路径

2> 规则:判断定义的路径是给谁用?判断请求从哪发出

1.给客户端浏览器使用,需要加虚拟目录(项目的访问路径)
建议虚拟目录动态获取:request.getContextPath();

eg:<a>,<form>,重定向

举例:

 String contextPath = request.getContextPath();
 //简单的重定向方法
  response.sendRedirect(contextPath+"/Response2");

2.给服务器使用,不需要加虚拟目录
eg: 转发路径

4.2 、服务器输出字符数据到浏览器

步骤:

1.获取字符输出流
2.输出数据

       //获取字符输出流
        PrintWriter pw = response.getWriter();
        //输出数据
        pw.write("你好啊,response");

注意:

乱码问题

        //获取流对象之前,设置流的默认编码
        response.setCharacterEncoding("utf-8");
        //告诉浏览器,服务器发送的消息体数据的编码,建议浏览器使用该编码解码
        response.setHeader("content-type","text/html;charset=utf-8");
        //简单的形式,设置编码
        response.setContentType("text/html;charset=utf-8");

最简单的操作,直接使用下句即可解决乱码:

   response.setContentType("text/html;charset=utf-8");

4.3 服务器输出字节码数据到浏览器

步骤:

1.获取字节输出流
2.输出数据

        response.setContentType("text/html;charset=utf-8");
        //获取字节输出流
        ServletOutputStream sos = response.getOutputStream();
        //输出数据
        sos.write("你好".getBytes("utf-8"));

4.4 验证码

html

<script>
/*
   点击超链接或者图片换一张
   1.给图片或者超链接绑定单机事件
   2.重新设置图片 src 属性值
 */
window.onload=function () {
//获取图片对象
 var img = document.getElementById("tupian");
 //绑定单击事件
    img.onclick=function () {
        //加时间戳
        var date=new Date().getTime();
        img.src="/day/Checkcode?"+date;
    }
}
    </script>
</head>
<body>
<img id="tupian" src="/day/Checkcode">
<a  id="change" href="">看不清换一张?</a>
</body>

servlet

@WebServlet("/Checkcode")
public class Checkcode extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int width = 100;
        int height = 50;

        //1.创建一个对象,在内存中画图
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //2.美化图片
        //填充背景色
        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 = 0; 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.orange);
        //随机生成坐标点
        for(int i=0;i<10;i++){
            int x1=ran.nextInt(width);
            int x2=ran.nextInt(width);
            int y1=ran.nextInt(width);
            int y2=ran.nextInt(width);
            g.drawLine(x1,x2,y1,y2);

        }
        //3.将图片输出到页面展示
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

效果展示:点击图片切换验证码


image.png

相关文章

网友评论

    本文标题:JavaWeb——response

    本文链接:https://www.haomeiwen.com/subject/aijdtctx.html