JavaWeb工程中url地址的写法

两种url地址:

1. "/"给服务器使用, 代表web工程根路径(webroot)
2. "/"给浏览器使用, 代表tomcat 目录下的webapps文件夹

注意:

1. 开发中一般不使用绝对地址
2. 开发中只要是写url地址, 都以/开头

举例:

 1 // servletcontext
 2 this.getServletContext().getRealPath("/image/apple.png"); // 服务器
 3 
 4 // forward 转发
 5 this.getServletContext().getRequestDispatcher("/register.html"); // 服务器
 6 
 7 // sendRedirect 重定向
 8 response.sendRedirect("/burgundyred/register.html"); // 浏览器
 9 
10 // 页面超链接
11 <a href="/burgundyred/servlet/Demo.do">点击</a> // 浏览器
12 <a href="${pageContext.request.contextPath }/servlet/Demo.do">点击</a> 

拓展: 使用//还是\?

1. 读取url资源使用// 

e.g. C:\abd\asd


2. 读取硬盘上的资源使用\ 

e.g. http://

原文地址:https://www.cnblogs.com/shaohsiung/p/9535678.html