jsp中路径的问题。。。
2013-11-24 21:07
357 查看
刚刚学jsp的时候都是从看别人的代码,模仿着做,但是这样也有看不懂的地方,这个相对路径和绝对路径就让我纠结了好久。。所以我自己弄了一个demo实验了一下,试验出结果了,但是不明白原理,纠结了一天,终于问别人知道了,最关键的是我自己看到“地址栏”才理解啊。。。所有不能离了题。先附上demo的代码:
index.jsp
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的图片
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的图片
相关文章推荐
- JS的正则表达式(一)
- JSTL详解
- 几种location.href的区别 js实现网页防止被iframe框架嵌套功能
- js跳转页面方法
- js实现图片切换效果图
- javascript中的函数作用域和声明提前
- JavaScript instanceof的实现
- 巧妙利用函数的惰性载入提高javascript 代码性能
- JS将图片存储到数组中,滚动时加载
- Javascript模拟继承(赠送.net吐槽一段)
- JSF select One/Many
- JS settimeout 解析
- JS中的中文在jsp页面中乱码的解决方法
- json 总结
- jsp 乱码问题原因
- C#和Javascript的try…catch…finally的一点差别
- JS常用事件以及节点操作
- JS验证框架(exValidation)
- 35-JavaScript-DOM-document对象
- js