Request & Response

一 概述

从浏览器输入一个地址访问服务器,或者将浏览器封装的内容发送到服务器,这个过程称为是请求Request。服务器接收到请求,进行处理,处理后将结果封装后显示回浏览器端,这个过程称为是响应Response。

二 Response API 使用演示

response.setStatus(int sc) 设置响应状态码

200 正确

302 重定向

304 查找本地缓存

404 请求资源不存在

500 服务器内部错误

public class ResponseDemo1 extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 设置响应的状态码:
        // response.setStatus(404);
        // 完成重定向
        // response.setStatus(302);
        // 2 设置响应头
        // response.setHeader("Location", "/web01/ResponseDemo2");
     //实际开发中可以使用response.sendRedirect(“/web01/ResponseDemo2”);替换重定向写法

        
        // 3 定时刷新
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("5秒以后页面跳转!");
        response.setHeader("Refresh", "5;url=/web01/ResponseDemo2");
    }

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

}

 *扩展:使用JS实现页面读秒跳转:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Refresh" content="5;url=/web01/response/demo2.html">
<title>Insert title here</title>
<script type="text/javascript">
    var i = 5;
    function load(){
        window.setInterval("changeSeconds()",1000);
    }
    
    function changeSeconds(){
        i--;
        document.getElementById("span1").innerHTML=i;
    }
</script>
</head>
<body onload="load()">
<h1>demo1.html</h1>
<h3>页面将在<span id="span1">5</span>秒后跳转到demo2.html</h3>
</body>
</html>

三 Response 中文乱码处理

1 使用字节流输出中文是否会产生乱码?

  不一定。如果中文转成字节数组用的字符集与浏览器默认字符集不一致,则可能产生乱码

  解决方法:将中文转成字节数组时用的字符集和浏览器默认字符集设置一致即可。

2 使用字符流输出中文是否会产生乱码?

  一定乱码。字符流有缓冲区,response获得字符流,默认缓冲区编码是ISO-8859-1。该字符集不支持中文。

  解决方法:将response获得字符流缓冲区的编码和浏览器默认字符集设置一致即可。

public class ResponseDemo3 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        test2(response);
    }
    /**
     * 使用字符流输出中文
     */
    private void test2(HttpServletResponse response) throws IOException, UnsupportedEncodingException {
        //此时会产生乱码
        //response.getWriter().println("中文");
        
        // 解决方法:设置浏览器默认打开的时候采用的字符集:
        // response.setHeader("Content-Type", "text/html;charset=UTF-8");
        // 设置response获得字符流的缓冲区的编码:
        // response.setCharacterEncoding("UTF-8");
        
        // 将上面两行代码简化
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("中文");
    }
    /**
     * 使用字节流输出中文
     */
    private void test1(HttpServletResponse response) throws IOException, UnsupportedEncodingException {
        // 使用字节流的方式输出中文:
        ServletOutputStream outputStream = response.getOutputStream();
        //此时会产生乱码
        //outputStream.wirte("中文".getBytes());
        
        // 解决方法:设置浏览器默认打开的时候采用的字符集
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        // 设置中文转成字节数组字符集编码
        outputStream.write("中文".getBytes("UTF-8"));
    }

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

}

*扩展:字节流与字符流的区别详解 https://www.cnblogs.com/DONGb/p/7844123.html

先来看一下流的概念:

在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。

程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。

字节流与字符流

在java.io包中操作文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream完成,输入使的是InputStream,在字符流中输出主要是使用Writer类完成,输入流主要使用Reader类完成。(这四个都是抽象类)

 

java中提供了专用于输入输出功能的包Java.io,其中包括:
     InputStream,OutputStream,Reader,Writer
     InputStream 和OutputStream,两个是为字节流设计的,主要用来处理字节或二进制对象,
     Reader和 Writer.两个是为字符流(一个字符占两个字节)设计的,主要用来处理字符或字符串.


字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好!如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点
     所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以形成字节序列

      字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串; 2. 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以
       字节流是最基本的,所有的InputStrem和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的 但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化 这两个之间通过 InputStreamReader,OutputStreamWriter来关联,实际上是通过byte[]和String来关联 在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的 

==================我们还可以看到:============
Reader类的read()方法返回类型为int :作为整数读取的字符(占两个字节共16位),范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1


inputStream的read()虽然也返回int,但由于此类是面向字节流的,一个字节占8个位,所以返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。因此对于不能用0-255来表示的值就得用字符流来读取!比如说汉字.

 
操作流程

在Java中IO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:

1 使用File类打开一个文件

2 通过字节流或字符流的子类,指定输出的位置

3 进行读/写操作

4 关闭输入/输出

IO操作属于资源操作,一定要记得关闭


字节流 字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是OutputStream、InputStream 字节输出流:OutputStream OutputStream是整个IO包中字节输出流的最大父类,此类的定义如下:
public abstract class OutputStream extends Object implements Closeable,Flushable 从以上的定义可以发现,此类是一个抽象类,如果想要使用此类的话,则首先必须通过子类实例化对象,那么如果现在要操作的是一个文件,则可以使用:FileOutputStream类。通过向上转型之后,可以为OutputStream实例化 Closeable表示可以关闭的操作,因为程序运行到最后肯定要关闭 Flushable:表示刷新,清空内存中的数据 FileOutputStream类的构造方法如下: public FileOutputStream(File file)throws FileNotFoundException 写数据: public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建 String str="Hello World"; byte[] b=str.getBytes(); out.write(b);//因为是字节流,所以要转化成字节数组进行输出 out.close(); } } 也可以一个字节一个字节进行输出,如下: public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建 String str="Hello World"; byte[] b=str.getBytes(); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } } 以上输出只会进行覆盖,如果要追加的话,请看FileOutputStream类的另一个构造方法: public FileOutputStream(File file,boolean append)throws FileNotFoundException 在构造方法中,如果将append的值设置为true,则表示在文件的末尾追加内容。 public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f,true);//追加内容 String str="\r\nHello World"; byte[] b=str.getBytes(); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } } 文件中换行为:\r\n 字节输入流:InputStream 既然程序可以向文件中写入内容,则就可以通过InputStream从文件中把内容读取进来,首先来看InputStream类的定义: public abstract class InputStream extends Object implements Closeable 与OutputStream类一样,InputStream本身也是一个抽象类,必须依靠其子类,如果现在是从文件中读取,就用FileInputStream来实现。 观察FileInputStream类的构造方法: public FileInputStream(File file)throws FileNotFoundException 读文件: public class Test12 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int len=in.read(b); in.close(); System.out.println(new String(b,0,len)); } } 但以上方法是有问题的,用不用开辟这么大的一个字节数组,明显是浪费嘛,我们可以根据文件的大小来定义字节数组的大小,File类中的方法:public long length() public class Test13 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[(int) f.length()]; in.read(b); in.close(); System.out.println(new String(b)); } } 我们换种方式,一个字节一个字节读入~ public class Test14 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[(int) f.length()]; for(int i=0;i<b.length;i++){ b[i]=(byte) in.read(); } in.close(); System.out.println(new String(b)); } } 但以上情况只适合知道输入文件的大小,不知道的话用如下方法: public class Test15 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int temp=0; int len=0; while((temp=in.read())!=-1){//-1为文件读完的标志 b[len]=(byte) temp; len++; } in.close(); System.out.println(new String(b,0,len)); } } 字符流 在程序中一个字符等于两个字节,那么java提供了Reader、Writer两个专门操作字符流的类。 字符输出流:Writer Writer本身是一个字符流的输出类,此类的定义如下: public abstract class Writer extends Object implements Appendable,Closeable,Flushable 此类本身也是一个抽象类,如果要使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileWriter的子类。 FileWriter类的构造方法定义如下: public FileWriter(File file)throws IOException 字符流的操作比字节流操作好在一点,就是可以直接输出字符串了,不用再像之前那样进行转换操作了。 写文件: public class Test16 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Writer out=new FileWriter(f); String str="Hello World"; out.write(str); out.close(); } } 在默认情况下再次输出会覆盖,追加的方法也是在构造函数上加上追加标记 public class Test17 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Writer out=new FileWriter(f,true);//追加 String str="\r\nHello World"; out.write(str); out.close(); } } 字符输入流:Reader Reader是使用字符的方式从文件中取出数据,Reader类的定义如下: public abstract class Reader extends Objects implements Readable,Closeable Reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用FileReader子类。 FileReader的构造方法定义如下: public FileReader(File file)throws FileNotFoundException 以字符数组的形式读取出数据: public class Test18 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Reader input=new FileReader(f); char[] c=new char[1024]; int len=input.read(c); input.close(); System.out.println(new String(c,0,len)); } } 也可以用循环方式,判断是否读到底: public class Test19 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Reader input=new FileReader(f); char[] c=new char[1024]; int temp=0; int len=0; while((temp=input.read())!=-1){ c[len]=(char) temp; len++; } input.close(); System.out.println(new String(c,0,len)); } } 字节流与字符流的区别 字节流和字符流使用是非常相似的,那么除了操作代码的不同之外,还有哪些不同呢? 字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的 字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close方法的话,则不会输出任何内容,说明字符流用的是缓冲区,并且可以使用flush方法强制进行刷新缓冲区,这时才能在不close的情况下输出内容 那开发中究竟用字节流好还是用字符流好呢? 在所有的硬盘上保存文件或进行传输的时候都是以字节的方法进行的,包括图片也是按字节完成,而字符是只有在内存中才会形成的,所以使用字节的操作是最多的。 如果要java程序实现一个拷贝功能,应该选用字节流进行操作(可能拷贝的是图片),并且采用边读边写的方式(节省内存)。

 四 Request API 使用演示

public class RequestDemo1 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得请求方式:
        System.out.println("请求方式:"+request.getMethod());
        // 获得客户机的IP地址:
        System.out.println("客户机IP地址:"+request.getRemoteAddr());
        // 获得请求参数的字符串
        System.out.println("请求参数的字符串:"+request.getQueryString());
        // 获得请求路径的URL和URI
        System.out.println("请求路径的URL:"+request.getRequestURL());
        System.out.println("请求路径的URI:"+request.getRequestURI());
        // 获得请求头的信息
        System.out.println("获得客户机浏览器类型:"+request.getHeader("User-Agent"));
    }

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

}

五 Request对象接收表单请求参数

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>request接收表单参数</h1>
<form action="/web01/RequestDemo2" method="post">
    用户名:<input type="text" name="username"/><br/>
    密码:<input type="password" name="password"><br/>
    性别:<input type="radio" name="sex" value="man"/><input type="radio" name="sex" value="woman"/><br/>
    籍贯:<select name="city">
        <option value="beijing">北京市</option>
        <option value="shanghai">上海市</option>
        <option value="shenzhen">深圳市</option>
    </select><br/>
    爱好:<input type="checkbox" name="hobby" value="basketball"/>篮球
    <input type="checkbox" name="hobby" value="football"/>足球
    <input type="checkbox" name="hobby" value="volleyball"/>排球<br/>
    自我介绍:<textarea name="info" rows="3" cols="8"></textarea><br/>
    <input type="submit" value="提交">
</form> 
</body>
</html>
public class RequestDemo2 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收用户名和密码:
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 接收性别和籍贯:
        String sex = request.getParameter("sex");
        String city = request.getParameter("city");
        // 接收爱好:
        String[] hobby = request.getParameterValues("hobby");
        // 接收自我介绍
        String info = request.getParameter("info");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        System.out.println("性别:"+sex);
        System.out.println("籍贯:"+city);
        System.out.println("爱好:"+Arrays.toString(hobby));
        System.out.println("自我介绍:"+info);
        
        // 使用getParameterMap接收数据:
        Map<String, String[]> map = request.getParameterMap();
        for (String key:map.keySet()) {
            String[] value = map.get(key);
            System.out.println(key+"    "+Arrays.toString(value));
        }
    }

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

}

 六 Request对象接收表单请求参数的中文乱码处理

1 post方式提交数据产生乱码的原因:post方式提交的数据在请求体中,request对象接收到数据之后,放入request的缓冲区中。缓冲区默认编码ISO-8859-1不支持中文

解决方案:修改request的缓冲区的默认编码

2 get方式产生乱码原因:get方式提交的数据在请求行的url后面,在地址栏上已经进行了一次URL编码。

解决方案:将存入到request缓冲区中的值以ISO-8859-1的方式获取到,再以UTF-8的方式进行解码。

3 实际开发中,因为get方式提交的数据会显示在地址栏上,且有长度限制,一般使用post方式提交表单数据

public class RequestDemo3 extends HttpServlet {
    /**
     * 演示get方式处理中文乱码
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收数据:
        // request.setCharacterEncoding("UTF-8");

        String name = request.getParameter("name");
        /*String encode = URLEncoder.encode(name, "ISO-8859-1");
        String decode = URLDecoder.decode(encode, "UTF-8");
        System.out.println("姓名:"+decode);*/
        //简化代码
        String value = new String(name.getBytes("ISO-8859-1"),"UTF-8");
        System.out.println("姓名:"+value);
    }

    /**
     * 演示post方式处理中文乱码
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        // 接收数据:
        // 设置缓冲区的编码
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        System.out.println("姓名:"+name);
    }

}

猜你喜欢

转载自www.cnblogs.com/createtable/p/10121387.html