web.xml的配置
//欢迎页的配置
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置自定义错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
jsp中路径相关的获取
- 获取项目的根路径
request.getContextPath()
- 获取当前页面的全路径
request.getRequestURL()
- 项目根路径到当前页面的路径
request.getRequesetURL()
- 获取页面所在服务器的全路径
application.getRealPath("目标页面");
实践:
两个问题:
-
页面转发,静态资源地址错误问题
-
项目根路径改变导致访问地址错误
-
转发
地址以”/“开头,例如”/user/login.jsp“ 真实的转发地址会变成
http://localhost:8080/项目根路径/user/login.jsp
- 重定向
地址以”/“开头,例如”/user/login.jsp“,真实的地址:
http://localhost:8080/user/login.jsp
这样子请求不到对应的请求,需要加上根路径
通过request.getContextPath()实现
- 静态资源
地址以”/“开头,例如”/static/css01.css“,真实地址
http://localhost:8080/static/css01.css
需要加上项目的根路径