您的位置:首页 > Web前端 > JavaScript

jsp中路径的问题。。。

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

    index.jsp

public class TiaoZhuanServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String info=request.getParameter("info");
request    .setAttribute("info", info);
request.getRequestDispatcher("show.jsp").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);

}


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的图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: