当前位置:编程学习 > JSP >>

做了个jsp&servlet路径状态的测试,花了偶几个小时,把结果贴出共享

答案:PS:这里怎么贴html阿,害得我还得格式化文本!




路径状态测试

目的
测试jsp之间互相跳转,servlet跳转jsp环境下绝对路径和相对路径的状态
寻找出一种通用的解决办法


资源

打成的WAR包
MyWAR

组成文件
JSP
a.jsp

b/
    b.jsp
    ccc.jsp


Servlet

TestServlet.java       mapping: /layer1/layer2/TestServlet


流程描述
1. 由a.jsp直接跳转到b.jsp
   或
   由a.jsp提交到TestServlet,TestServlet根据a的提交选择相应跳转方式(forward/sendRedirect)
   然后跳转到b.jsp
2. b.jsp包含一系列超链,来显示不同方式的跳转后超链的情况
   /a.jsp
   ../a.jsp
   ../../a.jsp
   ../../../a.jsp

   ccc.jsp
   /b/ccc.jsp
   ../ccc.jsp
   ../b/ccc.jsp
   ../../ccc.jsp
   ../../b/ccc.jsp
   ../../../ccc.jso
   ../../../b/ccc.jsp

   [contextPath]/b/b.jsp



测试结果


短语替换

http://localhost:8080/           =>  ip/
/MyWAR/                          =>  war/
http://localhost:8080/MyWAR/     =>  ip/war/
/MyWAR                           =>  [contextPath]  (request.getContextPath())





跳转方式              直接跳转<a ..>            servlet/forward                       servlet/sendRedirect
                                               requestDispatcher.forward             response.sendRedirect
---------------------+---------------------+----------------------------------+------------------------
servlet中跳转的路径    x                    /b/b.jsp                            /war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------
跳转后浏览器的地址栏   ip/war/b/b.jsp       ip/war/layer1/layer2/TestServlet    ip/war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------
b.jsp中的链接列表       浏览器状态条出现的链接地址
<a href=>---------------------+---------------------+----------------------------------+------------------------
/a.jsp                 ip/a.jsp              ip/a.jsp                          ip/a.jsp
../a.jsp               ip/war/a.jsp          ip/war/layer1/a.jsp               ip/war/a.jsp
../../a.jsp            ip/a.jsp              ip/war/a.jsp                      ip/a.jsp
../../../a.jsp         ip/a.jsp              ip/a.jsp                          ip/a.jsp

ccc.jsp                ip/war/b/ccc.jsp      ip/war/layer1/layer2/ccc.jsp      ip/war/b/ccc.jsp
/b/ccc.jsp             ip/b/ccc.jsp          ip/b/ccc.jsp                      ip/b/ccc.jsp
../ccc.jsp             ip/war/ccc.jsp        ip/war/layer1/ccc.jsp             ip/war/ccc.jsp
../b/ccc.jsp           ip/war/b/ccc.jsp      ip/war/layer1/b/ccc.jsp           ip/war/b/ccc.jsp
../../ccc.jsp          ip/ccc.jsp            ip/war/ccc.jsp                    ip/ccc.jsp
../../b/ccc.jsp        ip/b/ccc.jsp          ip/war/b/ccc.jsp                  ip/b/ccc.jsp
../../../ccc.jsp       ip/ccc.jsp            ip/ccc.jsp                        ip/ccc.jsp
../../../b/ccc.jsp     ip/b/ccc.jsp          ip/b/ccc.jsp                      ip/b/ccc.jsp


[contextPath]/b/b.jsp  ip/war/b/b.jsp        ip/war/b/b.jsp                    ip/war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------




总结

由上面的列表看出,只有最下面一行是有实际意义而且无论以任何方式跳转链接地址都是正确无误的
1. 在jsp中的“/”表示的根为“ip”,不包含WAR路径
2. 在servlet中以forward方式使用的“/”表示的根为“ip/war/”,包含了WAR路径
3. 在servlet中以sendRedirect方式使用的“/”表示的根为ip,不包含WAR路径
4.

上一个:贴一个偶写的分页类
下一个:关于 HTTP应答头的问题 tshhgd 请看

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,