获得Jsp相对路径的办法

比如http://www.jdon.com:81/jive/temp/index.jsp中
如果要获得/jive/temp/这个路径,通过正常途径是比较难的。
Servlet API没有提供直接获得这种路径的办法,现在有一个解决办法如下:

//获得/jive/temp/index.jsp
String pathname = request.getRequestURI();
//去处index.jsp
File tempfile = new File(pathname).getParentFile();
//获得/jive/temp/
String realbase =pathname.substring(0,tempfile.toString().length()+1);
out.println("<br>"+"==="+realbase);

其他人有更好的主意吗?

我想出了一个办法,可以用StringTokenizer类。

代码如下:
String pathName = request.getRequestURI();
StringTokenizer st=new StringTokenizer(pathName,":80/");//分离成两部分
String state = st.nextToken();//得到了www.jdon.com
String event = st.nextToken();//得到了jive/temp/index.jsp

虽然不是直接得到,但是比你的简单,对么?

好主意 但是有个缺点 端口一定是80 经常可能是其他如81 或8080

那就先取它的端口,不过这样会变得很麻烦,脱离实际了。

或者直接以“/”分离,然后。。。。

好像这样就行:
<%= request.getContextPath() + "/" %>

你这是获得的是容器的路径,即是/jive/
而/jive/temp/就无法获得,我测试过几次

这样再试试:
<%= request.getRequestURI().substring(0, request.getRequestURI().length() - config.getServletName().length()) %>

你这个办法有点类似我那个办法,不知道如果存在端口81或8080
getServername.length()能不能计算在内

request.getRequestURI() : "/jive/temp/index.jsp"
config.getServletName() : "index.jsp"

out.println("<br>"+pathname.substring(0,pathname.lastIndexOf("/")+1)) ;