java获取web路径(java 获取路径)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享java获取web路径的知识,其中也会对java 获取路径进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
java 怎么获取web根目录
以工程名为TEST为例: \x0d\x0a\x0d\x0a(1)得到包含工程名的当前页面全路径:request.getRequestURI() \x0d\x0a结果:/TEST/test.jsp \x0d\x0a(2)得到工程名:request.getContextPath() \x0d\x0a结果:/TEST \x0d\x0a(3)得到当前页面所在目录下全名称:request.getServletPath() \x0d\x0a结果:如果页面在jsp目录下 /TEST/jsp/test.jsp \x0d\x0a(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") \x0d\x0a结果:D:/resin/webapps/TEST/test.jsp \x0d\x0a(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();\x0d\x0a结果:D:/resin/webapps/TEST \x0d\x0a\x0d\x0a2.在类中取得路径: \x0d\x0a\x0d\x0a(1)类的绝对路径:String u=Class.class.getClass().getResource("/").getPath() \x0d\x0a结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ \x0d\x0a(2)得到工程的路径:System.getProperty("user.dir") \x0d\x0a结果:D:/TEST \x0d\x0a\x0d\x0a3.在Servlet中取得路径: \x0d\x0a\x0d\x0a(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 \x0d\x0a结果:E:/Tomcat/webapps/TEST \x0d\x0a(2)得到IE地址栏地址:request.getRequestURL() \x0d\x0a结果: \x0d\x0a(3)得到相对地址:request.getRequestURI() \x0d\x0a结果:/TEST/test
在java中怎么获取页面的路径
这里面我把se跟ee方面获取路径的给你列举出来了,希望对你有用
Java中使用的路径,分为两种:绝对路径和相对路径。归根结底,Java本质上只能使用绝对路径来寻找资源。所有的相对路径寻找资源的方法,都不过是一些便利方法。不过是API在底层帮助我们构建了绝对路径,从而找到资源的!
在开发Web方面的应用时, 经常需要获取服务器中当前WebRoot的物理路径。
如果是Servlet , Action , Controller, 或者Filter , Listener , 拦截器等相关类时, 我们只需要获得ServletContext, 然后通过ServletContext.getRealPath("/")来获取当前应用在服务器上的物理地址。
如果在类中取不到ServletContext时,有两种方式可以做到:
1)利用Java的类加载机制:调用 XXX.class.getClassLoader().getResource(""); 方法来获取到ClassPath , 然后处理获得WebRoot目录。
这种方式只能是该class在WebRoot/WEB-INF/classes下才能生效, 如果该class被打包到一个jar文件中, 则该方法失效。这时就应该用下面一种方式。
2)spring框架的思路,在WEB-INF/web.xml中,创建一个webAppRootKey的param,指定一个值(默认为webapp.root)作为键值,然后通过Listener, 或者Filter,或者Servlet 执行String webAppRootKey = getServletContext().getRealPath("/"); 并将webAppRootKey对应的webapp.root 分别作为Key,Value写到System Properties系统属性中。之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径。
根据第二种的思路,我们还可以再扩展一下。不过对于在部署在一台服务器中的应用来说,若还不是你所需请再往下看。
下面是一些得到classpath和当前类的绝对路径的一些方法。你可使用其中的一些方法来得到你需要的资源的绝对路径:
1.DebitNoteAction.class.getResource("")
得到的是当前类FileTest.class文件的URI目录。不包括自己!
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
atacarnet/src/com/evi/modules/atacarnet/action/
2.DebitNoteAction.class.getResource("/")
得到的是当前的classpath的绝对URI路径。
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
3.Thread.currentThread().getContextClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
推荐使用该方法获取。
4.DebitNoteAction.class.getClassLoader().getResource("") 或ClassLoader.getSystemResource("")
得到的也是当前ClassPath的绝对URI路径。
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
5.取得服务器相对路径
System.getProperty("user.dir")
例如:E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin
6.取得项目中的绝对路径
一般用request.getRealPath("/")或request.getRealPath("/config/")
但现在不提倡使用request.getRealPath("/")了,大家可试用ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
要取得src的文件非常容易,因为src是默认的相对目录,比如你说要取得src下com目录的test.java文件,你只需要这样就够了
File f = new File(com/test.java);
但如果我要取得不在src目录或者WebRoot目录下的文件呢,而是要从src或者WebRoot同级的目录中取呢,比如说doc吧。
我的硬方法是这样实现的:
String path = this.getServletContext().getRealPath("/");
Properties p = new Properties();
p.load(new FileInputStream(new File(path.substring(0,(path.lastIndexOf("\\WebRoot") + 1)) + "doc/db.properties")));
System.out.println(p.getProperty("driverName"));
-------------------------------
另:Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
假定你的web application 名称为news,你在浏览器中输入请求路径:
则执行下面向行代码后打印出如下结果:
1、 System.out.println(request.getContextPath());
打印结果:/news
2、System.out.println(request.getServletPath());
打印结果:/main/list.jsp
3、 System.out.println(request.getRequestURI());
打印结果:/news/main/list.jsp
4、System.out.println(request.getRequestURL());
打印结果:
5、 System.out.println(request.getRealPath("/"));
打印结果:F:\Tomcat 6.0\webapps\news\test
java如何得到项目的webRoot 路径?
使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:
1、使用JSP Servlet取得WEB根路径可以用request.getContextPath(),相对路径request.getSession().getServletContext().getRealPath("/"),它们可以使用我们很容易取得根路径。
2、如果使用了spring, 在WEB-INF/web.xml中,创建一个webAppRootKey的param,指定一个值(默认为webapp.root)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext().getRealPath("/"); 并将webAppRootKey对应的webapp.root分别作为Key,Value写到System Properties系统属性中。之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径。
具体示例代码如下:
web.xml
?xml version="1.0" encoding="UTF-8"?
web-app version="2.4"
xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
"
context-param
param-namewebAppRootKey/param-name
param-valuecsc2.root/param-value
/context-param
listener
listener-classtest.ApplicationListener/listener-class
/listener
/web-app
ApplicationListener.java
package test;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
public class ApplicationListener extends ContextLoaderListener {
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
String webAppRootKey = sce.getServletContext().getRealPath("/");
System.setProperty("csc2.root" , webAppRootKey);
String path =System.getProperty("csc2.root");
System.out.println("sssss:::"+path);
}
}
test.java
public class test {
public void remve(){
String path =System.getProperty("csc2.root");
System.out.println("result::::::::"+path);
}
}
index.jsp
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%@ page import="java.util.*" %
%@ page import="test.test" %
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
%
test t = new test();
t.remve();
%
html
/html
部署程序发布 启动TOMCAT 运行index.jsp 就可以调用JAVA中全局设置的物理路径了(说明这里的JSP 只是调用了TEST.JAVA 的remove方法,不做其他使用。原理解释,TOMCAT启动和读取WEB.XML 监听方式加载SPRING ApplicationListener继承SPRING ContextLoaderListener加载SPRING顺便吧全局路径赋值给csc2.root 描述,这样之后JAVA 代码中就可以使用System.getProperty("csc2.root")调用全路路径了。

关于java获取web路径和java 获取路径的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
