jsp中路径的问题。。。

      刚刚学jsp的时候都是从看别人的代码,模仿着做,但是这样也有看不懂的地方,这个相对路径和绝对路径就让我纠结了好久。。所以我自己弄了一个demo实验了一下,试验出结果了,但是不明白原理,纠结了一天,终于问别人知道了,最关键的是我自己看到“地址栏”才理解啊。。。所有不能离了题。先附上demo的代码:

    index.jsp

<FORM  action="servlet/TiaoZhuanServlet" method="post">
          请输入内容:<input type="text" name="info">
          <input type="submit" value="提交">
          </FORM>
View Code

    show.jsp:

    <%
                          String info=(String)request.getAttribute("info");
                   %>
                   <h3>info的内容是:<%=info%></h3>
View Code

web.xml:

1 <servlet>
2     <servlet-name>TiaoZhuanServlet</servlet-name>
3     <servlet-class>org.gsh.wh.servlet.TiaoZhuanServlet</servlet-class>
4   </servlet>
5 
6   <servlet-mapping>
7     <servlet-name>TiaoZhuanServlet</servlet-name>
8     <url-pattern>/servlet/TiaoZhuanServlet</url-pattern>
9   </servlet-mapping>
View Code

servlet:

 1 public class TiaoZhuanServlet extends HttpServlet {
 2 
 3 
 4     private static final long serialVersionUID = 1L;
 5 
 6     public void doGet(HttpServletRequest request, HttpServletResponse response)
 7             throws ServletException, IOException {
 8         request.setCharacterEncoding("GBK");
 9         String info=request.getParameter("info");
10         request    .setAttribute("info", info);
11         request.getRequestDispatcher("show.jsp").forward(request, response);
12     }
13 
14 
15      
16     public void doPost(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18                 this.doGet(request, response);
19 
20     }
View Code

web.xml中德配置<servlet>     <description>This is the description of my J2EE component</description>     <display-name>This is the display name of my J2EE component</display-name>     <servlet-name>TiaoZhuanServlet</servlet-name>     <servlet-class>org.gsh.wh.servlet.TiaoZhuanServlet</servlet-class>   </servlet>

  <servlet-mapping>     <servlet-name>TiaoZhuanServlet</servlet-name>     <url-pattern>/servlet/TiaoZhuanServlet</url-pattern>   </servlet-mapping>  在加“/”和不加“/”的理解上是这样的:加了“/”就是绝对路径。而不加“/”就是相对路径 绝对路径:比如在“/show”,那么它会在http://localhost/Test2下面照“show.jsp”, 如果不加的话:就是相对路径:从http://localhost/Test2/servlet/TiaoZhuanServlet(a)往show.jsp跳转,相对a的话跳转后的就是: http://localhost/Test2/servlet/show.jsp此时肯定找不到show.jsp

而如果你的配置如下: <servlet>     <description>This is the description of my J2EE component</description>     <display-name>This is the display name of my J2EE component</display-name>     <servlet-name>TiaoZhuanServlet</servlet-name>     <servlet-class>org.gsh.wh.servlet.TiaoZhuanServlet</servlet-class>   </servlet>

  <servlet-mapping>     <servlet-name>TiaoZhuanServlet</servlet-name>     <url-pattern>/TiaoZhuanServlet</url-pattern>   </servlet-mapping> 表单那 里的action="TiaoZhuanServlet",记得更改。

则相对路径是:http://localhost/Test2下面照“show.jsp”,而不加的话:依然跳转http://localhost/Test2/TiaoZhuanServlet 往"show.jsp"跳转,结果为:http://localhost/Test2/show.jsp,所以加和不加一样

demo的图片

原文地址:https://www.cnblogs.com/wuhao1991/p/3440492.html