request和response的知识

来源:互联网 发布:我的世界如何查看端口 编辑:程序博客网 时间:2024/05/18 01:26
[java] view plaincopy
  1. public class Demo1 extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         doPost(request, response);  
  6.     }  
  7.       
  8.     //在servlet中用outputstream输出中文的问题  
  9.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  10.             throws ServletException, IOException {  
  11.         test4(response);  
  12.     }  
  13.     private void test4(HttpServletResponse response)  
  14.     throws IOException, UnsupportedEncodingException {  
  15.         //如果servle的代码写成这样"response.getOutputStream().write(1);",因为  
  16.         //浏览器默认的编码是gb2312,那么它会去寻找编号为1所对应的字符,结果是为"",  
  17.         //如果写成字符串"1"的话,那么这个1字符是经过getBytes之后的,所以会直接输出1  
  18.         response.getOutputStream().write(1);  
  19.         response.getOutputStream().write("1".getBytes());  
  20.     }  
  21.     private void test3(HttpServletResponse response)  
  22.     throws IOException, UnsupportedEncodingException {  
  23.         String name="中国3";  
  24.         //如果程序把text/html后面的;写成了,的话,那么浏览器会提示下载此servlet文件  
  25.         response.setHeader("Content-type""text/html,charset=UTF-8");  
  26.         response.getOutputStream().write(name.getBytes("UTF-8"));  
  27.     }  
  28.     private void test2(HttpServletResponse response)  
  29.         throws IOException, UnsupportedEncodingException {  
  30.         String name="中国2";  
  31.         //用html技术中的meta标签来模拟http的响应头,来控制浏览器的行为  
  32.         response.getOutputStream().write("<meta http-equiv='content-type' content='text/type;charset=UTF-8'>".getBytes());  
  33.         response.getOutputStream().write(name.getBytes("UTF-8"));  
  34.     }  
  35.     private void test1(HttpServletResponse response)  
  36.             throws IOException, UnsupportedEncodingException {  
  37.         String name="中国1";  
  38.         //程序以什么码表输出了,程序就要控制浏览器以什么样的码表打开  
  39.         response.setHeader("Content-type""text/html;charset=UTF-8");  
  40.         //response.getOutputStream().write(name.getBytes());  
  41.         response.getOutputStream().write(name.getBytes("UTF-8"));  
  42.     }  
  43. }  
[java] view plaincopy
  1. public class Demo2 extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         doPost(request, response);  
  6.     }  
  7. //通过response的wirter流输出数据  
  8.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.         //因为在servlet传递给浏览器的过程中是通过response进行编码后传递的,而老外默认是  
  11.         //使用iso8859-1来进行编码传递的,所以我们需要对response的编码进行设置,以控制  
  12.         //response以什么码表向浏览器写出数据  
  13.         //测试得知,在设置response的编码时最好写在上面  
  14.           
  15.         //第一种方式,控制response的编码和浏览器显示的编码,因为浏览器默认是gb2312的  
  16.         //response.setCharacterEncoding("utf-8");  
  17.         //下面这句通过response设置浏览器的编码,其实默认同时也把response的编码也给设置了,所以上面的那句话也可以省略掉了  
  18.         //response.setContentType("text/html;charset=utf-8");  
  19.           
  20.         //第二种方式,控制response的编码与浏览器的一致,也就是gb2312编码  
  21.         response.setCharacterEncoding("gb2312");  
  22.           
  23.         String name="中国";  
  24.         PrintWriter out=response.getWriter();  
  25.         //第三种方式,不设置response的编码,使用默认的iso8859-1,然后把string转化为8859-1后进行传递  
  26.         //out.print(new String(name.getBytes(),"iso8859-1"));  
  27.           
  28.         out.print(name);  
  29.           
  30.     }  
  31. }  

[java] view plaincopy
  1. //文件下载  
  2. public class Demo3 extends HttpServlet {  
  3.   
  4.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  5.             throws ServletException, IOException {  
  6.         doPost(request, response);  
  7.     }  
  8.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.         String path=this.getServletContext().getRealPath("/download/小破孩.jpg");  
  11.         String filename=path.substring(path.lastIndexOf("/")+1);  
  12.         //如果下载文件是中文文件,那么文件名需要经过url编码  
  13.         response.setHeader("content-disposition""attachment;filename="+URLEncoder.encode(filename,"utf-8"));  
  14.         InputStream is=new FileInputStream(path);  
  15.         OutputStream os=response.getOutputStream();  
  16.         int len=0;  
  17.         byte[] bs=new byte[1024];  
  18.         while((len=is.read(bs))>0){  
  19.             os.write(bs, 0, len);  
  20.         }  
  21.         os.close();  
  22.         is.close();  
  23.     }  
  24. }  

[java] view plaincopy
  1. public class Demo5 extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         doPost(request, response);  
  6.     }  
  7.   
  8.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.         //test1(response);  
  11.         //test2(response);  
  12.         test3(request,response);  
  13.     }  
  14.     //实用的跳转技术,最终的信息还是要在浏览器中显示比较好,这样的话容易排版,test2中是输出的是直接页面的源代码  
  15.     private void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {  
  16.           
  17.         String message="<meta http-equiv='refresh' content='3;url=/testweb/index.jsp'>恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,<a href=\"/index.jsp\">请点击此处</a>";  
  18.         request.setAttribute("message", message);  
  19.         this.getServletContext().getRequestDispatcher("/forword.jsp").forward(request, response);  
  20.           
  21.     }  
  22.     private void test2(HttpServletResponse response) throws IOException {  
  23.         //假设这是一个用于登录的Servlet  
  24.         //假设程序运行到此,用户登录成功了  
  25.         response.setContentType("text/html;charset=gb2312");  
  26.         response.setHeader("refresh""3;url='/testweb/index.jsp'");  
  27.         response.getWriter().write("恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,<a href=\"/index.jsp\">请点击此处</a>");  
  28.     }  
  29.   
  30.     private void test1(HttpServletResponse response) throws IOException {  
  31.         response.setHeader("refresh""3");//每隔三秒刷新一次  
  32.         int data=new Random().nextInt(1000);  
  33.         response.getWriter().println(data);  
  34.     }  
  35.   
  36. }  

[java] view plaincopy
  1. public class Demo5 extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         //使用expries缓存当前内容  
  6.         response.setDateHeader("expires", System.currentTimeMillis()+1000*3600);  
  7.         String data="bbbbbbbbbbbbbbbbbb";  
  8.         System.out.println("访问---");  
  9.         response.getWriter().write(data);  
  10.     }  
  11.   
  12.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  13.             throws ServletException, IOException {  
  14.         doGet(request, response);  
  15.     }  
  16.   
  17. }  

[java] view plaincopy
  1. /** 
  2.  * 重定向的特点: 
  3.  * 1.浏览器会向服务器发送两次请求,意味着就有两个request/response 
  4.  * 2.用重定向技术,地址栏会发生变化 
  5.  * 
  6.  *用户登录和购物车时,通常会用到重定向技术 
  7.  * 
  8.  *同时调用getOutPutStream()和getWriter()会抛出异常 
  9.  */  
  10. public class Demo6 extends HttpServlet {  
  11.   
  12.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  13.             throws ServletException, IOException {  
  14.         /*sendRedirect的内部原理 
  15.             response.setStatus(302); 
  16.             response.setHeader("location", "/testweb/index.jsp"); 
  17.         */  
  18.         response.sendRedirect("/testweb/index.jsp");  
  19.     }  
  20.   
  21.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  22.             throws ServletException, IOException {  
  23.         doGet(request, response);  
  24.     }  
  25.   

0 0
原创粉丝点击