* web.xml中<url-pattern>路径,(叫它Servlet路径!)
> 要么以“*”开关,要么为“/”开头
* 转发和包含路径
> *****以“/”开头:相对当前项目路径,例如:http://localhost:8080/项目名/ request.getRequestdispacher("/BServlet").for...();
> 不以“/”开头:相对当前Servlet路径。 request.getRequestdispacher("/BServlet").for...();,假如当前Servlet是:http://localhost:8080/项目名/servlet/AServlet,
就是http://localhost:8080/项目名/servlet/BServlet
* 重定向路径(客户端路径)
> 以“/”开头:相对当前主机,例如:http://localhost:8080/, 所以需要自己手动添加项目名,例如;response.sendRedirect("/day10_1/Bservlet");
* 页面中超链接和表单路径
> 与重定向相同,都是客户端路径!需要添加项目名
> <form action="/day10_1/AServlet">
> <a href="/day10_/AServlet">
> <a href="AServlet">,如果不以“/”开头,那么相对当前页面所在路径。如果是http://localhost:8080/day10_1/html/form.html。 即:http://localhost:8080/day10_1/html/ASevlet
> *****建立使用以“/”开头的路径,即绝对路径!
* ServletContext获取资源路径
> 相对当前项目目录,即当前index.jsp所在目录
* ClassLoader获取资源路径
> 相对classes目录
* Class获取资源路径
> 以“/”开头相对classes目录
> 不以“/”开头相对当前.class文件所在目录。
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;