自己在WEB学习过程中遇到的问题

问题一:

下面这两段代码差别不大,为何test1结果不同:

代码1:

 1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 2         test(response);
 3         test1(response);
 4     }
 5 
 6     //这样写会乱码,返回两个? 原因是外国人默认用ISO8859码表,当数据存到response中时,ISO8859中没有汉字对应的码表,所以返回?所以解决乱码的关键在于码表的更换,
 7     public void test(HttpServletResponse response) throws IOException{
 8         String data = "中国";
 9         PrintWriter out = response.getWriter();
10         out.write(data);
11         
12     }
13     //解决test里面的乱码问题
14     public void test1(HttpServletResponse response) throws IOException{
15         //设置response用的码表是UTF-8,以控制response以什么码表像浏览器写出数据
16         response.setCharacterEncoding("UTF-8");
17         //因为浏览器默认GB2312,所以现在必须控制浏览器打开数据的码表为UTF-8
18         response.setHeader("content-type","text/html;charset=UTF-8");
19         String data = "中国";
20         PrintWriter out = response.getWriter();
21         out.write(data);
22     }

代码2:

 1 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 2         doGet(request,response);
 3     }
 4     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 5         //test(response);
 6         test1(response);
 7     }
 8 
 9     //这样写会乱码,返回两个? 原因是外国人默认用ISO8859码表,当数据存到response中时,ISO8859中没有汉字对应的码表,所以返回?所以解决乱码的关键在于码表的更换,
10     public void test(HttpServletResponse response) throws IOException{
11         String data = "中国";
12         PrintWriter out = response.getWriter();
13         out.write(data);
14         
15     }
16     //解决test里面的乱码问题
17     public void test1(HttpServletResponse response) throws IOException{
18         //设置response用的码表是UTF-8,以控制response以什么码表像浏览器写出数据
19         response.setCharacterEncoding("UTF-8");
20         //因为浏览器默认GB2312,所以现在必须控制浏览器打开数据的码表为UTF-8
21         response.setHeader("content-type","text/html;charset=UTF-8");
22         String data = "中国";
23         PrintWriter out = response.getWriter();
24         out.write(data);
25     }

两段代码区别在于:第一段test和test1方法同时调用,输出结果是????

         第二段只调用了test1方法,结果输出 “中国”

为什么?????

问题二:

 下列代码,想从浏览器访问对应位置的图片,为什么总显示NullPointException

 1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
 2         //1、先设置浏览器要以什么方式打开
 3         response.setHeader("Content-type","image/jpeg");
 4         //2、再把目标文件加载成一个输入流  输入流才有读的方法   F://10.jpg这样写是错误的
 5         InputStream in = this.getServletContext().getResourceAsStream("F://10.jpg");
 6         //3、定义字符数组长度的中间变量
 7         int len = 0;
 8         //4、定义一个字符数组
 9         byte by[] = new byte[1024];
10         //5、先定义好一个输出到浏览器的流
11         //6、把输入流里的东西读入到字符数组中 
12         OutputStream out = response.getOutputStream();
13             while((len = (in.read(by)))!=-1){
14                 
15                 out.write(by,0,len);
16                 out.flush();
17             }
18             out.close();
19             in.close();    
20     }

当把"F://10.jpg"改成“10.jpg”就对了???是不能指定图片所在路径吗?(F盘下有10.jpg,web应用下也有10.jpg)

原文地址:https://www.cnblogs.com/rgever/p/7929912.html