请求路径的问题
请求路径的问题
明确路径谁使用:
- 浏览器使用:需要加载虚拟目录(项目访问路径)
- 服务端使用:不需要加载虚拟目录
练习:
<a href= '路径'>
加载虚拟目录<form action= '路径'>
加载虚拟目录- req.getRequestDispatcher("路径") 不加载虚拟目录
- req.sendRedirect("路径") 加虚拟目录
动态获取虚拟目录的方式
String contextPath = request.getContextPath();
response.sendDirect(contextPath + "资源B的路径");