request 对象和 response 对象

Posted 刘二雄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了request 对象和 response 对象相关的知识,希望对你有一定的参考价值。

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象

HttpServletResponse

HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。

1.向客户端输出中文数据(字节)

复制代码
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test5(response);
    }
    
    //输出整数:最好转换成字符串输出
    private void test5(HttpServletResponse response) throws IOException{
        int i = 98;
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //以字节流用默认编码向客户端输出中文数据:没有乱码
    //默认的是GBK
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes());
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    //以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        //通知客户端查看UTF-8码表
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        ServletOutputStream out = response.getOutputStream();
        out.write("<meta http-equiv=\'Content-Type\' content=\'text/html;charset=UTF-8\'>".getBytes("UTF-8"));
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test4(HttpServletResponse response) throws IOException{
        String s = "你好!四";
        response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码

分析:出现乱码的图解

 

解决办法:

1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)

2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码

3)通过meta标签模拟请求头:out.write("<meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\' />".getBytes());

4)通过以下方法:response.setContentType("text/html;charset=UTF-8");

2.向客户端输出中文数据(字符)

复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo2 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test3(response);
    }

    //字符流输出中文数据
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        response.setCharacterEncoding("UTF-8");    //设置查的UTF-8(对内容进行编码,查utf-8)
        response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
        PrintWriter out = response.getWriter();
        out.write(s);//默认查的是ISO-8859-1码表
    }
    //字符流输出中文数据
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }
    //字符流输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码

分析:

乱码原因:以默认编码发送数据 ISO-8859-1(没有中国二字编码),此时会发生乱码

解决办法:

1)

setCharacterEncoding(“UTF-8”);//更改编码为UTF-8

response.setHead(“Context-type”,”text/html;charset=UTF-8”);//告诉客户端编码方式

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

在字符流输出中文数据时:

response.setContentType("text/html;charset=utf-8");有两个作用:

通知字符流以UTF-8编码输出

通知客户端以UTF-8解码显示

3.输出随机验证码

复制代码
package cn.lsl.response;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        
        int width = 120;
        int height = 25;
        //创建一副内存图像BufferedImage
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到属于该图片的画笔:Graphics();
        Graphics g = image.getGraphics();
        //画边框
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width, height);
        //填充背景色
        g.setColor(Color.YELLOW);
        g.fillRect(1, 1, width-2, height-2);
        //画干扰线
        g.setColor(Color.GRAY);
        Random r = new Random();
        for(int i=0; i<10; i++)
            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
        //随机数字
        g.setColor(Color.RED);
        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
        int x = 23;
        for(int i=0; i<4; i++){
            g.drawString(r.nextInt(10)+"", x, 20);
            x = x+20;
        }
        //输出到浏览器的页面上:ImageIO
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码

4.定时刷新

复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo4 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test2(response);
    }
    
    //定时刷新到自己
    private void test1(HttpServletResponse response) throws IOException{
        Random r = new Random();
        int i = r.nextInt();
        response.setIntHeader("Refresh", 1);
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //刷到别的页面
    private void test2(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setHeader("Refresh", "2;URL=/Response/index.jsp");
        out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href=\'/Response/index.jsp\'>这里</a>");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码

5.控制缓存时间

复制代码
package cn.lsl.response;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

importRequest对象和Response对象

[JavaWeb-HTTP]request对象和response对象的原理

Request和Response

request 对象和 response 对象

request对象和response对象

08Request和Response