
images的url写成src="${pageContextrequestcontextPath}/images/logojpg"
一般会把${pageContextrequestcontextPath}这部分提取出来,在jsp写成:
<% String path = requestgetContextPath();%>
如图:
则images路径可以写成src="<%=path%>/images/logojpg"
访问loginjsp页面的话,写成>
一、用Jsp获取
String file="文件";(例如:datamdb)
String path=applicationgetRealPath(file);
2、获取文件的绝对路径
String p2=requestgetRequestURI();
3、获取当前jsp页面的路径
String p3=requestgetContextPath();
4、获取当前项目的路径
String p4=requestgetServletPath();
二、用Java类获取
1、获取Eclipse路径
String a1=SystemgetProperty("userdir");
2、获取当前的classpath路径
String a2=类名classgetResource("")toString();
String a3=DBConnectionclassgetResource("/")toString();
String a4=DBConnectionclassgetClassLoader()getResource("")toString();
String t=ThreadcurrentThread()getContextClassLoader()getResource("")getPath();
3、获取文件的绝对路径
如果要获取WebContent目录下的文件绝对路径怎么办?可以用下面的方法
String t=ThreadcurrentThread()getContextClassLoader()getResource("")getPath();
int num=tindexOf("metadata");
String path=tsubstring(1,num)replace('/', '\\')+"项目名\\WebContent\\文件";
三、用servlet获取
1、获取项目的绝对路径
requestgetSession()getServletContext()getRealPath("")
2、获取浏览器地址
requestgetRequestURL()
3、获取当前文件的绝对路径
requestgetSession()getServletContext()getRealPath(requestgetRequestURI())
在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用SystemgetProperty("userdir")获取工程的绝对路径。
另:在Jsp,Servlet,Java中详细获得路径的方法!
1jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:requestgetRequestURI()
结果:/TEST/testjsp
(2)得到工程名:requestgetContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:requestgetServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/testjsp
(4)得到页面所在服务器的全路径:applicationgetRealPath("页面jsp")
结果:D:\resin\webapps\TEST\testjsp
(5)得到页面所在服务器的绝对路径:absPath=new javaioFile(applicationgetRealPath(requestgetRequestURI()))getParent();
结果:D:\resin\webapps\TEST
2在类中取得路径:
(1)类的绝对路径:ClassclassgetClass()getResource("/")getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:SystemgetProperty("userdir")
结果:D:\TEST
3在Servlet中取得路径:
(1)得到工程目录:requestgetSession()getServletContext()getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:requestgetRequestURL()
结果:>
在Tomcat的安装目录下有一个conf文件夹
在这个文件夹里面打开serverxml文件
在里面添加上一句话:
<Context path="/sell" reloadable="true" docBase="D:\eclipse of JavaEE\workspace02\sell" workDir="D:\eclipse of JavaEE\workspace02\sell\work" />
其中 path="/你的工程名" docBase="你的工程的绝对路径" workDir="你的工程的绝对路径/work"
其他都不变 ,这样就可以部署很多域名访问配置了
在Servlet中,指定服务器上某个文件夹来保存文件:
// 获得服务器应用程序所在的绝对路径String realPath = thisgetServletContext()getRealPath(thisgetServletName());
realPath = realPathsubstring(0, realPathlastIndexOf("\\"));
String uploadPath = realPath + "\\upload\\"; // 用于存放上传文件的服务器目录绝对路径
String projectPath = thisgetClass()getResource("/")getPath()substring(1)replace() + "Server-Configurationxml";
web应用运行时指向的是你tomcat目录/webapps/应用/web-inf/classes/Server-Configurationxml
望采纳!
注意,如projectPath中空格的经过base64编码转换后变成了"%20",你还得replace("%20" , " ")。
以上就是关于Java web项目访问路径问题。求解啊!全部的内容,包括:Java web项目访问路径问题。求解啊!、D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core、java中怎样获取当前路径的绝对路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)