一.相对路径
- 相对路径,与绝对路径一般在链接中
- 只要不以 / 开头都是相对路径,相对路径是从当前资源出发找到其他资源的过程,例如:
reqest.getRequestDispatcher("index.jsp").forward(request,response);
二.绝对路径(全路径)
- 只要路径中以/开头叫做全路径,从项目根路径(WebContent)出发找其他资源的过程
reqest.getRequestDispatcher("/index.jsp").forward(request,response);
三. 在Servlet的注解中的 /
我们在生成Servlet文件后,会有注解:
@WebServlet(value = {"/a/b/ShowServlet"})
下面是全部代码
@WebServlet(value = {"/a/b/ShowServlet"})
public class ShowServlet extends HttpServlet {
private PeopleService peopleService = new PeopleServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<People> list = peopleService.show();
request.setAttribute("list",list);
reqest.getRequestDispatcher("/index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
- 对于Servlet,我们在启动项目后如果要先经过ShowServlet ,会在浏览器中先
http://localhost:8080/people/a/b/ShowServlet
,(项目名为people),获取信息,然后跳转至index.jsp
,这里的index.jsp
是绝对路径
四. 连起来运用(易出错dian)
4.1 先直接在WebContent下新建一个文件夹images放一张照片aa.PNG
4.2 直接在WebContent下新建index.jsp(只展示重点代码)
<body>
<img src="images/aa.PNG" />
hello
</body>
可以看出index.jsp可以获取到aa.PNG,运行得到如下页面:
4.3 结合上面已经有的ShowServlet 代码,先到servlet(图片无法加载)
- 经过servlet跳转,得到下面页面
- 可以看出,图片加载不出来了,因为现在在
http://localhost:8080/people/a/b/ShowServlet
目录下,会找a/b
/下的images/aa.PNG
,但是这是不存在的,所以出现404错误
4.4 修改index.jsp中的图片路径(关于重定向,项目名)
- 如果是重定向,静态资源引用,必须
<img src=""/> <a href=""> <script src=""/> css
引用时,其中 / 都表示tomcat的webapps文件夹目录,webapps文件夹下才是你的项目文件夹 - 即服务器根目录,下面是项目文件夹
- 修改代码,将其设置为
\项目名\图片路径
<img src="\people\images\aa.PNG" />
- 但是要注意项目名,在idea中一般你没有改的话,在配置Tomcat时你的名字时类如
people_war_exploded
这样的,在你的浏览器中也可以看到:http://localhost:8080/people_war_exploded/show
,要注意项目名统一
- 最终结果
4.5 关于相对路径(servlet中的重定向与路径)
- 如果将上面的servlet的重定向变为相对路径这样
reqest.getRequestDispatcher("index.jsp").forward(request,response);
- 根据前面的分析,知道会在
Webcontent/a/b/ShowServlet
这个虚拟目录里找index.jsp,出现404错误,因为index.jsp
直接在Webcontent
目录下
@WebServlet(value = {"/a/b/ShowServlet"})
public class ShowServlet extends HttpServlet {
private PeopleService peopleService = new PeopleServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<People> list = peopleService.show();
request.setAttribute("list",list);
reqest.getRequestDispatcher("index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
- 如何不用绝对路径,而跳转成功呢,可以用往上跳一层目录的方法:
../
表示相对当前目录往外跳一层目录,但是一般不用,在项目中路径一般都用绝对路径,有利于项目的修改 request.getRequestDispatcher("../../index.jsp").forward(request,response);
- 表示跳出了
b目录,a目录
,然后到达Webcontent
目录下,这样效果与之前一样
4.6 项目结构
五.关于图片路径问题
下面四种形式均可以加载出来,与 / \ 符号无关(本人不知道为什么)
<img src="images/aa.PNG"/>
<img src="/people/images/aa.PNG" />
<img src="\people\images\aa.PNG" />
<img src="images\aa.PNG"/>
六.关于网页显示流程问题
- 服务器将html变为文件流,发送给客户端
- 客户端进行解析,这时发现一个静态资源图片
- 就向客户端请求图片
- 一共有两个请求:index.jsp;aa.PNG
七.关于web.xml中的/
- 项目根目录:WebConcent, src(所有的构建路径,构建路径可以是多个的,不止一个src,可以通过Sorce Folder新建)
例如:
Webconcent中有一个文件index.jsp(目录下直接有一个文件)
ser中有一个Servlet.java - 如果:index.jsp中请求
<a href="abc">...</a>
,则 寻找范围:既会在src根目录中找 也会在WebContent根目录中找 - 如果:index.jsp中请求
<a href="a/abc"></a>
,寻找范围:先在src或WebContent中找a目录,然后再在a目录中找abc - ** / 代表的功能 **:
- web.xml中的 /:代表项目根路径
http://localhost:8080/Servlet2.5Project/
- == jsp中的/==: 服务器根路径(返回上一级)
http://localhost:8080/