路径

一、响应编码

 

        当使用response.getWriter()来向客户端发送字符数据时,如果在之前没有设置编码,那么默认使用iso-8859-1,因为iso-8859-1不支持中文,一定乱码。
        在使用response.getWriter()之前可以使用response.setCharaceterEncoding()来设置字符流的编码为gbk或utf-8,当然我们通常会选择utf-8。这样使用response.getWriter()发送的字符就是使用utf-8编码的。但还是会出现乱码!因为浏览器并不知道服务器发送过来的是什么编码的数据!这时浏览器会使用gbk来解码,所以乱码!
        在使用response.getWriter()之前可以使用response.setHeader("Content-type","text/html;charset=utf-8")来设置响应头,通知浏览器服务器这边使用的是utf-8编码,而且在调用setHeader()后,还会自动执行setCharacterEncding()方法。这样浏览器会使用utf-8解码,所以就不会乱码了!
        在静态页面中,使用<meta>来设置content-type响应头,例如:

        还有一种简写方式  setContentType("text/html;charset=utf-8);   推荐使用这种方法

 

            <meta http-equiv="content-type" content="text/html; charset=UTF-8">

二、请求编码

        * 客户端发送给服务器的请求参数是什么编码:

    客户端首先要打开一个页面,然后在页面中提交表单或点击超链接!在请求这个页面时,服务器响应的编码是什么,那么客户端发送请求时的编码就是什么。
    * 服务器端默认使用什么编码来解码参数:
    服务器端默认使用ISO-8859-1来解码!所以这一定会出现乱码的!因为iso不支持中文!
    * 请求编码处理分为两种:GET和POST:GET请求参数不在请求体中,而POST请求参数在请求体中,所以它们的处理方式是不同的!
        * GET请求编码处理:

        > String username = new String(request.getParameter("iso-8859-1"), "utf-8");

        > 在server.xml中配置URIEncoding=utf-8
        * POST请求编码处理:
        > String username = new String(request.getParameter("iso-8859-1"), "utf-8");

        > 在获取参数之前调用request.setCharacterEncoding("utf-8");

三、URL编码

        表单的类型:Content-Type: application/x-www-form-urlencoded,就是把中文转换成%后面跟随两位的16进制。

  为什么要用它:在客户端和服务器之间传递中文时需要把它转换成网络适合的方式。
  * 它不是字符编码!
  * 它是用来在客户端与服务器之间传递参数用的一种方式!
  * URL编码需要先指定一种字符编码,把字符串解码后,得到byte[],然后把小于0的字节+256,再转换成16进制。前面再添加一个%。
  * POST请求默认就使用URL编码!tomcat会自动使用URL解码!
  * URL编码:String username = URLEncoder.encode(username, "utf-8");
  * URL解码:String username = URLDecoder.decode(username, "utf-8");
  最后我们需要把链接中的中文参数,使用url来编码!

四、路径

    1.客户端路径

        超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:
                绝对路径;
                以“/”开头的相对路径;

                不以“/”开头的相对路径;

例如:http://localhost:8080/hello1/pages/a.html中的超链接和表单如下:

 
  1. 绝对路径:<a href="http://localhost:8080/hello2/index.html">链接1</a>

  2. 客户端路径:<a href="/hello3/pages/index.html">链接2</a>

  3. 相对路径:<a href="index.html">链接3</a>

  4. <hr/>

  5. 绝对路径:

  6. <form action="http://localhost:8080/hello2/index.html">

  7. <input type="submit" value="表单1"/>

  8. </form>

  9. 客户端路径:

  10. <form action="/hello2/index.html">

  11. <input type="submit" value="表单2"/>

  12. </form>

  13. 相对路径:

  14. <form action="index.html">

  15. <input type="submit" value="表单3"/>

  16. </form>

 

          链接1和表单1:没什么可说的,绝对路径;

 

      链接2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终访问的页面为http://localhost:8080/hello2/index.html;

        链接3和表单3:不以“/”开头,相对当前页面的路径,即a.html所有路径,即最终访问的路径为:http://localhost:8080/hello1/pages/index.html;

重定向1:

 
  1. public class AServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)

  3. throws ServletException, IOException {

  4. response.sendRedirect("/hello/index.html");

  5. }

  6. }

 

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

    因为路径以“/”开头,所以相对当前主机,即 http://localhost:8080/hello/index.html。

重定向2:

 
  1. public class AServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)

  3. throws ServletException, IOException {

  4. response.sendRedirect("index.html");

  5. }

  6. }

 

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径不以“/”开头,所以相对当前路径,即http://localhost:8080/hello/servlet/index.html

    2.服务器端路径

 

        服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:

 

            以“/”开头;

            不以“/”开头;

        其中请求转发、请求包含都是服务器端路径,服务器端路径与客户端路径的区别是:

            客户端路径以“/”开头:相对当前主机;

            服务器端路径以“/”开头:相对当前应用;

请求转发1:

 
  1. public class AServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)

  3. throws ServletException, IOException {

  4. request.getRequestDispatcher("/BServlet").forward(request, response);

  5. }

  6. }

 

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/hello/BServlet。

请求转发2:

 
  1. public class AServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)

  3. throws ServletException, IOException {

  4. request.getRequestDispatcher("BServlet").forward(request, response);

  5. }

  6. }

 

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径不以“/”开头,所以相对当前应用,即http://localhost:8080/hello/servlet/BServlet。

    3.<url-pattern>路径

        <url-pattern>必须使用“/”开头,并且相对的是当前应用。

    4.ServletContext获取资源

 

        必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。

        例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/hello/servlet/AServlet:

 
  1. public class AServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)

  3. throws ServletException, IOException {

  4. String path1 = this.getServletContext().getRealPath("a.txt");

  5. String path2 = this.getServletContext().getRealPath("/a.txt");

  6. System.out.println(path1);

  7. System.out.println(path2);

  8. }

  9. }

        path1和path2是相同的结果:http://localhost:8080/hello/a.txt

    5.Class获取资源

        Class获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。

 
  1. package cn.yfy;

  2.  
  3. import java.io.InputStream;

  4.  
  5. public class Demo {

  6. public void fun1() {

  7. InputStream in = Demo.class.getResourceAsStream("/a.txt");

  8. }

  9.  
  10. public void fun2() {

  11. InputStream in = Demo.class.getResourceAsStream("a.txt");

  12. }

  13. }

 

        fun1()方法获取资源时以“/”开头,那么相对的是当前类路径,即/hello/WEB-INF/classes/a.txt文件;

        fun2()方法获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在cn.yfy包下,所以资源路径为:/hello/WEB-INF/classes/cn/yfy/a.txt。

    6.ClassLoader获取资源

        ClassLoader获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。但无论是否以“/”开头,资源都是相对当前类路径。

 
  1. public class Demo {

  2. public void fun1() {

  3. InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt");

  4. }

  5.  
  6. public void fun2() {

  7. InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt");

  8. }

  9. }

        fun1()和fun2()方法的资源都是相对类路径,即classes目录,即/hello/WEB-INF/classes/a.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值