如果带WebRoot,那么js、css、img都应该放到WebRoot目录下,否则访问会有问题。千万不要放在WEB-INF下,因为WEB-INF下的内容只有服务器转发可以访问到,出于安全考虑。
如果不带有WebRoot目录,那么可以放在WEB-INF外面(建立的文件夹中)。
一、JSP、Servlet中的相对路径
a) 在JSP中
“/”代表站点(服务器)根目录http://127.0.0.1/
b) 在Servlet中
“/”代表Web应用的根目录http://127.0.0.1/JSP_Servlet_Path/
request.getRequestDispatcher("/a/a.jsp").forward(request, response);
相对路径/a/a.jsp中/相对于web应用的根目录,所以相当于请求跳转到绝对路径
response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");
因为重定向中的方法是传递给浏览器,用于重新发送请求的,而在浏览器端“/”代表,相对于站点根目录,所以这里必须要加上/JSP_Servlet_Path,这样浏览器重新请求的地址为:http://127.0.0.1/JSP_Servlet_Path/b/b.jsp
package com.jsp_servlet_path.rqq; import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class JSP_Servlet extends HttpServlet { @Override public voiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { doPost(request,response); } @Override public voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { System.out.println(request.getContextPath()); //请求转发,在服务器端,"/"代表(相对于)web应用http://localhost:8000/JSP_Servlet_Path/ //所以请求转发的绝对路径为http://localhost:8000/JSP_Servlet_Path/a/a.jsp // request.getRequestDispatcher("/a/a.jsp").forward(request,response); //请求重定向,将方法中的地址参数传递给浏览器,让浏览器重新发送请求, //"/"代表(相对于)服务器站点http://localhost:8000/ //所以相当于浏览器重新请求了绝对路径http://localhost:8000/JSP_Servlet_Path/b/b.jsp response.sendRedirect("/JSP_Servlet_Path/b/b.jsp"); //response.sendRedirect(request.getContextPath()+ "/b/b.jsp"); }}
二、JSP中加入basePath
<% String path = request.getContextPath(); String basePath= request.getScheme()+"://" +request.getServerName()+":" +request.getServerPort()+path+"/";%><%=basePath%>">
注:相当于所有的href相对路径前面都加入了:
http://localhost:8000/JSP_Servlet_Path/
三、JSP与Servlet相互访问
JSP_Servlet com.jsp_servlet_path.rqq.JSP_Servlet CopyOfJSP_Servlet com.jsp_servlet_path.rqq.CopyOfJSP_Servlet JSP_Servlet /servlet/JSP_Servlet CopyOfJSP_Servlet /servlet/CopyOfJSP_Servlet index.jsp
<%@ page language="java"import="java.util.*"pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%=basePath%>"> My JSP 'index.jsp' starting page This is index.jsp page. <%--href中隐含了basePath,因为 <%=basePath%>">--%> a.jsp(a/a.jsp) a.jsp(/JSP_Servlet_Path/a/a.jsp) c.jsp(a/c/c.jsp) e.jsp(a/c/e/e.jsp) JSP_Servlet(servlet/JSP_Servlet) JSP_Servlet(/JSP_Servlet_Path/servlet/JSP_Servlet) 请求JSP_Servlet,并转发给CopyOfJSP_Servlet
package com.jsp_servlet_path.rqq; import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class JSP_Servlet extends HttpServlet { @Override public voiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { doPost(request,response); } @Override public voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { System.out.println(request.getContextPath()); //请求转发,在服务器端,"/"代表(相对于)web应用http://localhost:8000/JSP_Servlet_Path/ //所以请求转发的绝对路径为http://localhost:8000/JSP_Servlet_Path/a/a.jsp // request.getRequestDispatcher("/a/a.jsp").forward(request,response); //请求重定向,将方法中的地址参数传递给浏览器,让浏览器重新发送请求, //"/"代表(相对于)服务器站点http://localhost:8000/ //所以相当于浏览器重新请求了绝对路径http://localhost:8000/JSP_Servlet_Path/b/b.jsp response.sendRedirect("/JSP_Servlet_Path/b/b.jsp"); //response.sendRedirect(request.getContextPath()+ "/b/b.jsp"); Stringstr_i = request.getParameter("i"); int i =new Integer(str_i); switch(i) { case 0: request.getRequestDispatcher("/index.jsp").forward(request,response); System.out.println("index.jsp"); break; case 1: request.getRequestDispatcher("/a/a.jsp").forward(request, response); System.out.println("a.jsp"); break; case 2: request.getRequestDispatcher("/b/b.jsp").forward(request,response); System.out.println("b.jsp"); break; case 3: request.getRequestDispatcher("/a/c/c.jsp").forward(request,response); System.out.println("c.jsp"); break; case 100: request.getRequestDispatcher("/servlet/CopyOfJSP_Servlet").forward(request,response); System.out.println("forward to CopyOfJSP_Servlet"); break; default: System.out.println("default"); return; } }}