随笔 - 3  文章 - 8  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔档案(8)

文章档案(1)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

一、普通类


String path1 = this.getClass().getClassLoader().getResource(".").getPath();     //取得根目录(classes)路径/F:/zzd/java/test/p_test/build/classes/


getResource("XXX")     //取得根目录下的目录、文件的路径(XX为目录、文件名/F:/zzd/java/test/p_test/build/classes/Notepad.class),更深的目录、文件不行

getResource("/")     //不行

String path = path1.substring(path1.indexOf('/'+ 1, path1.indexOf("/WEB-INF")) + "/tp/" + tpb.getWjm();    //必须path1.indexOf('/') + 1而不能1,因为path1可能以file:/开头



getClass().getName();     //包括包的类名

getClass().getPackage().getName();     //包名


二、Servlet


String filePath = this.getServletContext().getRealPath("/");    //只能得到web目录F:\zzd\java\softdown\build\web\,其他无效

getRealPath(".")           //无效

getRealPath("XXX")          //无效



String path = this.getClass().getClassLoader().getResource("/").getPath();    //得到类目录/F:/zzd/java/softdown/build/web/WEB-INF/classes/

getResource("XXX")        //(同“一”)

getResource(".")        //取得/D:/Tomcat/common/classes/

String s = request.getServletPath();    //得到本Servlet的web相对路径:/pathTest
String s = request.getRequestURI();    //得到本Servlet的web绝对路径:/strutsTest/pathTest


三、Action

String filePath = this.getServlet().getServletContext().getRealPath("/");    //得到web目录F:\zzd\java\softdown\build\web\


String path = this.getClass().getClassLoader().getResource("/").getPath();    //取得根目录(classes)路径/F:/zzd/java/test/strutsTest2/build/web/WEB- INF/classes/

getResource("XXX")         //同“一”,XXX可为classes目录下的文件,更深目录下的文件须写出完整的相对根目录(classes)的路径,如:com.myapp.struts

String path = p1.substring(p1.indexOf('/'+ 1, p1.lastIndexOf('/')) + "相对路径";    //得到的路径以'/'开头,所以需p1.indexOf('/') + 1处理;用p1.lastIndexOf('/')去掉最后的"/XXX"


三、JSP
只能得到web目录F:\zzd\java\test\strutsTest\build\web\,其他无效


String path = pageContext.getServletContext().getRealPath("");               //得到web目录F:\zzd\java\test\strutsTest\build\web(没有最后的“\”)

String path = pageContext.getServletContext().getRealPath("/");          //得到web目录F:\zzd\java\test\strutsTest\build\web


String strPathFile2 = application.getRealPath("");        //得到web目录F:\zzd\java\test\strutsTest\build\web(没有最后的“\”)

String strPathFile2 = application.getRealPath("/");       //得到web目录F:\zzd\java\test\strutsTest\build\web\

=======================
1、服务器端的地址

服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的,也就是说这时候在jsp和servlet中的相对地址应该是相对于你的web应用,即相对于http: //192.168.0.1/webapp/的。

其用到的地方有:

forwarder:
ervlet中的request.getRequestDispatcher(address);
这个address是在服务器端解析的,所以,你要 forwarder到a.jsp应该这么写:request.getRequestDispatcher(“/user/a.jsp”)这个/相对于当前的web应用webapp,其绝对地址就是:http://192.168.0.1/webapp/user/a.jsp。

sendRedirect:
在jsp中<%response.sendRedirect("/rtccp/user/a.jsp");%>

struts的配置文件中,forward路径应当以"/"开头
=====

对class程序文件来说,classes目录是根目录
posted on 2007-12-26 21:09 cccp21 阅读(1131) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: