路径问题

路径都使用 / 开头

客户端 (超链接,表单,重定向) / 开头相对   主机路径,  /代表主机

服务端 (转发,包含,servletcontext,url-pattern) / 开头相对    当前应用的路径 

class  classloader  相对 classes 路径,应用/WEB-INF/classes/....

一: servletcontext获取路径:

 WebRoot

    -META-INT

    -WEB-INF

        --lib

          ---b.txt

        --web.xml

    -a.txt

    -index.html

5.1 获取真实路径

还可以使用ServletContext对象来获取Web应用下的资源,例如在hello应用的根目录下创建a.txt文件,现在想在Servlet中获取这个资源,就可以使用ServletContext来获取。

 

l  获取a.txt的真实路径:String realPath = servletContext.getRealPath(“/a.txt”),realPath的值为a.txt文件的绝对路径:F:\tomcat6\webapps\hello\a.txt;

l  获取b.txt的真实路径:String realPath = servletContext.getRealPath(“/WEB-INF/b.txt”);

 

5.2 获取资源流

不只可以获取资源的路径,还可以通过ServletContext获取资源流,即把资源以输入流的方式获取:

l  获取a.txt资源流:InputStream in = servletContext.getResourceAsStream(“/a.txt”);

l  获取b.txt资源流:InputStream in = servletContext.getResourceAsStream(“/WEB-INF/b.txt”);

 

5.3 获取指定目录下所有资源路径

还可以使用ServletContext获取指定目录下所有资源路径,例如获取/WEB-INF下所有资源的路径:

       Set set = context.getResourcePaths("/WEB-INF");

       System.out.println(set);

[/WEB-INF/lib/, /WEB-INF/classes/, /WEB-INF/b.txt, /WEB-INF/web.xml]

 

注意,本方法必须以“/”开头!!!

 

 二.获取类路径下资源

 

       InputStream in = this.getClass().getResourceAsStream("/xxx.txt");

       System.out.println(IOUtils.toString(in));

       InputStream in = this.getClass().getClassLoader().getResourceAsStream("xxx.txt");

       System.out.println(IOUtils.toString(in));

 

l  Class类的getResourceAsStream(String path):

  • 路径以“/”开头,相对classes路径;
  • 路径不以“/”开头,相对当前class文件所有路径,例如在cn.itcast.servlet.MyServlet中执行,那么相对/classes/cn/itcast/servlet/路径;

l  ClassLoader类的getResourceAsStream(String path):

  • 相对classes路径;

 

 

路径

客户端路径

 绝对路径

 相对带/ 是相对当前主机路径

 不带相/对当前路径:此种不建议

 

服务器端路径

转发和包含   相对带/ 相对当前项目  

getServletContext都是针对当前项目

 

this.getClass().getResourceAsStream()//带/相对classes      不带 相对当前

this.getClassLoader().         都是相对classes目录

1 与路径相关的操作

l  超链接

l  表单

l  转发

l  包含

l  重定向

l  <url-pattern>

l  ServletContext获取资源

l  Class获取资源

l  ClassLoader获取资源

 

2 客户端路径

超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:

l  绝对路径;

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

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

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

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

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

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

<hr/>

绝对路径:

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

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

</form>

客户端路径:

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

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

</form>

相对路径:

<form action="index.html">

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

</form>

 

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

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

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

 

重定向1:

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

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

    }

}

 

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

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

 

重定向2:

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       response.sendRedirect("index.html");

    }

}

 

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

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

 

2.1 建议使用“/”

强烈建议使用“/”开头的路径,这说明在页面中的超链接和表单都要以“/”开头,后面是当前应用的名称,再是访问路径:

<form action="/hello/servlet/AServlet">

</form>

<a href="/hello/b.html">链接</a>

 

其中/hello是当前应用名称,这也说明如果将来修改了应用名称,那么页面中的所有路径也要修改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解!

 

在Servlet中的重定向也建议使用“/”开头。同理,也要给出应用的名称!例如:

response.sendRedirect("/hello/BServlet");

 

其中/hello是当前应用名,如果将来修改了应用名称,那么也要修改所有重定向的路径,这一问题的处理方案是使用request.getContextPath()来获取应用名称。

response.sendRedirect(request.getContextPath() + "/BServlet");

 

3 服务器端路径

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

l  以“/”开头;

l  不以“/”开头;

 

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

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

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

 

转发1:

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

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

    }

}

 

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

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

 

转发2:

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

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

    }

}

 

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

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

 

4 <url-pattern>路径

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

 

5 ServletContext获取资源

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

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

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

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

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

       System.out.println(path1);

       System.out.println(path2);

    }

}

 

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

 

6 Class获取资源

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

package cn.itcast;

 

import java.io.InputStream;

 

public class Demo {

    public void fun1() {

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

    }

   

    public void fun2() {

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

    }

}

 

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

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

 

7 ClassLoader获取资源

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

public class Demo {

    public void fun1() {

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

    }

   

    public void fun2() {

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

    }

}

 

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

posted @ 2017-04-13 18:03  上台阶  阅读(292)  评论(0编辑  收藏  举报