• JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(上)(各种乱码、验证码、重定向和转发)


     HttpServletResponse简介

    • Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象
    • request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了
    • HttpServletResponse对象服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。
     响应:
    * 响应行
    * 状态码:
    void setStatus(int sc) 设置状态码 常用的状态码:200、302、304、404、500

    * 响应头(key:value的形式,一个key对应一个value,一个key对应多个value)
    * 设置头信息
    void setHeader(String name, String value)  (一个key对应一个value)经常使用的
    setHeader("aa","bb");
    setHeader("aa","cc");
    结果:aa:cc

    void setIntHeader(String name, int value)  
    void setDateHeader(String name, long date) 值是毫秒值(int 秒long 毫秒)

    void addHeader(String name, String value)  (一个key对应多个value)
    addHeader("aa","bb");
    addHeader("aa","cc");
    结果:aa:bb,cc

    void addIntHeader(String name, int value)  
    void addDateHeader(String name, long date)  

    * 响应体
    ServletOutputStream getOutputStream()  字节输出流
    PrintWriter getWriter()  字符输出流

    HttpServletResponse继承自ServletResponse
     
     
     

    HttpServletResponse使用案例(一)——重定向(登录页面)

    登录页面重定向
    状态码302    响应头location
    需求:登录页面,用户名和密码的登录页面,用户名和密码都是admin,如果有一个不是,重定向到登录页面,重新登录。
    方法一:
        // 设置302的状态码
        response.setStatus(302);
        //  设置地址
        response.setHeader("location","/day10/response/login.html");
    方法二:使用自带的方法sendRedirect() 方法,(实质是封装了设置状态码302和头信息location的代码)
        response.sendRedirect("/day10/response/login.html");

    HttpServletResponse使用案例(二)——页面定时刷新(页面读秒操作)

    响应的头refresh
    <meta http-equiv="refresh" content="5;url=/day10/response/login.html"> (客户端meta中设置头信息refresh)
    response.setHeader("refresh", "5;url=/day6/hello.html"); (或者在服务器端为客户端设置实现)

    HttpServletResponse使用案例(三)——禁用浏览器缓存(三个头信息)

    禁用缓存就是禁止浏览器缓存当前文档内容,包含如下三个头信息:
    Cache-Control:no-cache
    Expires:-1   值是日期类型(setDateHeader)
    Pragma:no-cache  
    实例方法如下:
    // 设置response 的三个头信息
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    // Expires 要求格式 日期格式
    // 通过setDateHeader设置一个毫秒信息,自动转换日期格式
    response.setDateHeader("Expires", -1);

    HttpServletResponse使用案例(四)——向页面输出中文(字节流和字符流)

    (1)字节流输出中文 ServletOutputStream getOutputStream()
    • 乱码和浏览器的编码有关
    • 设置浏览器默认打开时候的编码集
    • 获取字符串byte数组时,传入一个编码集
        字节输出中文是否一定乱码呢?
         不一定乱码,但有时候会乱码,跟浏览器的编码有关
        解决方法:
           设置浏览器打开文件时所采用的的编码
               response.setHeader("Content-Type", "text/html;charset=UTF-8");
           获取字符串byte数组编码和打开文件时编码一致
               "哈喽我的".getBytes("UTF-8")
     
    (2)字符流输出中文 PrintWriter getWriter()  
      字符输出中文一定乱码,因为response的字符流缓冲区的编码,默认值是ISO-8859-1,是不支持中文的。
      解决方法:
    •   设置response缓冲区编码: response.setCharacterEncoding("UTF-8");
    •   设置浏览器打开文件时所采用的的编码:response.setHeader("Content-Type","text/html;charset=UTF-8");  
    •   简写方式:response.setContentType("text/html;charset=UTF-8");(等效于以上两句代码)
    (3)response生成响应的注意事项
    • getOutputStream和getWriter方法分别用于得到输出二进制数据、输出文本数据的ServletOutputStream、PrintWriter对象。
    • getOutputStream和getWriter这两个方法相互排斥,调用了其中任何一个方法后,就不能再调用另一方法。
    • Servlet程序向ServletOutputStream或PrintWriter对象中写入的数据将被Servlet引擎从response中获取,Servlet引擎将这些数据当做响应消息的正文,然后再与响应状态行和各响应头组合后输出到客户端。
    • Servlet的service方法结束后,Servlet引擎将检查getWriter或getOutputStream方法返回的输出流对象是否已经调用过close方法,如果没有,Servlet引擎Tomcat将调用close方法关闭该输出流对象。调用close的时候,应该会调用flushBuffer

    HttpServletResponse使用案例(五)——文件下载(字节流和字符流)

    (1)超链接下载
         如果浏览器不能识别的格式,会弹出下载窗口。但是如果浏览器识别的话,会默认打开文件。
    (2)后台程序下载(弹出下载窗口)
    • 设置头信息response.setHeader("Content-Disposition","attatchment;filename="+文件名称);
    • 通过response.getOutputStream()向浏览器输出
    文件下载
    [java] view plain copy
     
    1. <span style="font-size:18px;">文件下载  
    2. // Test1:  
    3. String realpath = this.getServletContext().getRealPath(  
    4. "/download/11.jpg");  
    5. String filename = realpath.substring(realpath.lastIndexOf("\") + 1);  
    6.   
    7. response.setHeader("content-disposition", "attachment;filename="+filename);  
    8.   
    9. FileInputStream fis = new FileInputStream(realpath);  
    10. int len = 0;  
    11. byte[] buf = new byte[1024];  
    12. OutputStream os = response.getOutputStream();  
    13. while((len=fis.read(buf))!=-1){  
    14. os.write(buf, 0, len);  
    15. }  
    16. fis.close();  
    17.   
    18. Test2:文件名中文  
    19. String realpath = this.getServletContext().getRealPath(  
    20. "/download/11.jpg");  
    21. String filename = realpath.substring(realpath.lastIndexOf("\") + 1);  
    22.   
    23. response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));  
    24.   
    25. FileInputStream fis = new FileInputStream(realpath);  
    26. int len = 0;  
    27. byte[] buf = new byte[1024];  
    28. OutputStream os = response.getOutputStream();  
    29. while((len=fis.read(buf))!=-1){  
    30. os.write(buf, 0, len);  
    31. }  
    32. fis.close();  
    33.   
    34. Test3:字符流下载  
    35. String realpath = this.getServletContext().getRealPath(  
    36. "/download/11.jpg");  
    37. String filename = realpath.substring(realpath.lastIndexOf("\") + 1);  
    38.   
    39. response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));  
    40.   
    41. FileReader fis = new FileReader(realpath);  
    42. int len = 0;  
    43. char[] buf = new char[1024];  
    44. PrintWriter os = response.getWriter();  
    45. while((len=fis.read(buf))!=-1){  
    46. os.write(buf, 0, len);  
    47. }  
    48. fis.close();  
    49. </span>  
     
    注意:下载文件时中文名乱码的问题:
    [java] view plain copy
     
    1. <span style="font-size:18px;">// 解决各浏览器的中文乱码问题  
    2.         String agent = request.getHeader("User-Agent");  
    3.         // 处理safari的乱码问题  
    4.         byte[] bytes = agent.contains("MSIE") ? filename.getBytes() : filename  
    5.                 .getBytes("UTF-8");  
    6.         filename = new String(bytes, "ISO-8859-1");// 各浏览器基本都支持ISO编码  
    7. </span>  

    HttpServletResponse使用案例(六)——图片验证码

    (1)输出图片验证码的大致步骤
    在内存中生成图片
       可以使用BufferedImage对象
       该对象可以获取画笔对象  getGraphics()
    随机生成字母或数字
       可以使用Random对象
    把生成的数字或字母写在图片上 (通过画笔中的方法)
    把图片生成到页面上   (可以使用ImageIO对象)
    (2)输出图片验证码的具体细节:
      1) 建立BufferedImage对象:指定图片的长度宽度和类型
         BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      2) 取得Graphics画笔对象,用来绘制图片
         Graphics  graphics = image.getGraphics();
      3) 绘制背景颜色
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0,0,width,hight);
      4) 绘制边界
        graphics.setColor(Color.BLUE);
        graphics.drawRect(0,0,width,height);
      5) 生成随机数
        Random random = new Random();
        random.nextInt(n); // 生成0到n(不包括n)之间的随机数 前闭后开
      6) 绘制干扰线
        graphics.drawLine(x1,y1,x2,y2);
      7)设置字体 
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        如果验证码是中文,要使用中文的字体库
        通过词库生成随机验证码内容
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        汉字:u4e00 —— u9fa5
        graphics.drawString(str, x, y);
     8)设置旋转
         Graphics2D graphics = (Graphics2D) image.getGraphics();
          graphics.rotate(theta, x, y);
     9) 释放此图形的上下文以及它使用的所有系统资源
           graphics.dispose();
     10) 通过ImageIO对象的write静态方法将图片输出 
         ImageIO.write(image, "jpg", resp.getOutputStream());
    (3)使用旋转的技巧
    通过Graphics2D对象的  void rotate(double theta, double x, double y) 画旋转。
    Theta代表弧度。
    弧度的公式:弧度=角度 * PI / 180
    // 加入字体旋转 角度-30 --- 30之间
       int jiaodu = random.nextInt(60) - 30;
    // 转换角度为弧度
       double theta = jiaodu * Math.PI / 180;
    // 获取img标签对象   获得src的属性  动态src属性赋值
      var img = document.getElementById("imgId");
      img.src = "/day08/response7?"+new Date().getTime();
     (4)代码示例
    [plain] view plain copy
     
    1. <span style="font-size:18px;">public void doGet(HttpServletRequest request, HttpServletResponse response)  
    2.             throws ServletException, IOException {  
    3.         // 禁止浏览器缓存 验证码图片  
    4.         // response.setHeader("cache-control", "no-cache");  
    5.         // response.setHeader("pragma", "no-cache");  
    6.         // response.setDateHeader("expires", -1);  
    7.   
    8.         // 生成图片。使用Java图形界面技术awt、swing 包  
    9.         // 1 创建内存中图片  
    10.         int width = 120;  
    11.         int height = 30;  
    12.         BufferedImage bufferedImage = new BufferedImage(width, height,  
    13.                 BufferedImage.TYPE_INT_RGB);  
    14.   
    15.         // 2 绘制图片背景色  
    16.         Graphics2D graphics2d = (Graphics2D) bufferedImage.getGraphics();  
    17.         // 指定颜色  
    18.         graphics2d.setColor(Color.GRAY);  
    19.         graphics2d.fillRect(0, 0, width, height);  
    20.   
    21.         // 3、绘制边框  
    22.         graphics2d.setColor(Color.yellow);  
    23.         graphics2d.drawRect(0, 0, width - 1, height - 1);  
    24.   
    25.         // 4、向图片中生成验证码内容  
    26.         graphics2d.setColor(Color.BLUE);  
    27.         graphics2d.setFont(new Font("新宋体", Font.BOLD, 18));  
    28.         // String word =  
    29.         // "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";  
    30.         // 如果使用汉字,字体必须要支持汉字  
    31.         String word = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6";  
    32.         Random random = new Random();  
    33.         int x = 5;  
    34.         for (int i = 0; i < 4; i++) {  
    35.             // 加入字体旋转 角度-30 --- 30之间  
    36.             int jiaodu = random.nextInt(60) - 30;  
    37.             // 转换角度为弧度  
    38.             double theta = jiaodu * Math.PI / 180;  
    39.   
    40.             int randomIndex = random.nextInt(word.length());// 生成下标  
    41.             char c = word.charAt(randomIndex);  
    42.             // 将字符写入图片  
    43.             graphics2d.rotate(theta, x, 20);  
    44.             graphics2d.drawString(c + "", x, 20);  
    45.             graphics2d.rotate(-theta, x, 20);  
    46.             x += 30;  
    47.         }  
    48.   
    49.         // 5 绘制干扰线  
    50.         graphics2d.setColor(Color.LIGHT_GRAY);  
    51.         int x1;  
    52.         int x2;  
    53.         int y1;  
    54.         int y2;  
    55.         for (int i = 0; i < 10; i++) {  
    56.             x1 = random.nextInt(width);  
    57.             x2 = random.nextInt(width);  
    58.             y1 = random.nextInt(height);  
    59.             y2 = random.nextInt(height);  
    60.   
    61.             graphics2d.drawLine(x1, y1, x2, y2);  
    62.         }  
    63.         // 释放内存中资源  
    64.         graphics2d.dispose();  
    65.   
    66.         // 生成图片到浏览器  
    67.         ImageIO.write(bufferedImage, "jpg", response.getOutputStream());  
    68.     }  
    69.   
    70.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
    71.             throws ServletException, IOException {  
    72.         doGet(request, response);  
    73.     }  
    74.   
    75.     public static void main(String[] args) {  
    76.         System.out.println('u9fa5');  
    77.         System.out.println('u9fa4');  
    78.         System.out.println('u9fa3');  
    79.     }  
    80.   
    81. HTML:  
    82. <script type="text/javascript">  
    83.     // 点击切换验证码原理 ,重新载入图片  
    84.     function change(){  
    85.         // 获得id为myimg对象 ,重新加载Servlet程序  
    86.         document.getElementById("myimg").src= "/day6/response7?"+new Date().getTime();  
    87.     }  
    88. </script>  
    89. </head>  
    90. <body>  
    91. <!-- 引用一张图片,显示网页上 -->  
    92. <img src="/day6/response7" onclick="change();" id="myimg" style="cursor: pointer;"/>  
    93. </body>  
    94.   
    95. </span>  

      HttpServletRequest对象简介

      HttpServletRequest对象代表客户端请求,当客户端通过HTTP协议访问服务器时,,HTTP请求中的所有信息都封装在这个对象中,我们可以通过这个对象的方法 获取客户端的请求信息。它继承于ServletRequest类
    通过Request对象进行的常用操作
    • 获取客户机信息
    • 获取请求头信息
    • 获取请求参数
    • 利用请求域传递对象
    • 重定向和转发的区别

    HttpServletRequest获取客户机信息

    • getRequestURL方法返回客户端发出请求的完整URL
    • getRequestURI 方法返回请求行中的资源名部分(包含虚拟路径)
    • getQueryString 方法返回请求行中的参数部分
    • getRemoteAddr 方法返回发出请求的客户机的IP地址
    • getMethod 得到客户机的请求方式
    • getContextPath 获取工程虚拟目录的名称
    URI和URL区别 ?
    URI:/day6/request1
    URL:http://localhost/day6/request1
    URI范围比URL大,http://localhost/day6/request1 是URL也是 URI ,/day6/request1 是URI 不是 URL
     
    如何获得当前请求 访问资源路径 ? 就是在服务器网站内部路径 
    uri : /day6/request1
    contextpath : /day6 
    request.getRequestURI().substring(request.getContextPath().length()); ------ /request1 

    // 获得请求方式
    System.out.println("请求方式:" + request.getMethod());
    // 获得协议
    System.out.println("协议:" + request.getProtocol());
    // 获得请求资源路径
    System.out.println("请求路径:" + request.getRequestURI());
    System.out.println("请求路径:" + request.getRequestURL());
    // 获得请求行中参数
    System.out.println("参数:" + request.getQueryString());
    // 打印客户机IP
    System.out.println("ip:" + request.getRemoteAddr());
    // 获得工程虚拟路径名称
    System.out.println("虚拟路径名称:" + request.getContextPath());
     
    from: http://www.tk4479.net/u013087513/article/details/54587528
  • 相关阅读:
    机器学习任务攻略
    2.0 线性模型_李宏毅2021
    PyTorch
    jupyter切换虚拟环境
    python中yield的用法详解——最简单,最清晰的解释
    Full卷积、Same卷积、Valid卷积、带深度的一维卷积
    计算机环境变量的配置,以java为例以及eclipse简要设置
    conda创建/移除虚拟环境
    bak
    JSoup抓取本地页面
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/7045673.html
Copyright © 2020-2023  润新知