• .jsp获取路径(转)


    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

    <title>Untitled Document</title>

    </head><body>

    当前WEB应用的物理路径:<%=application.getRealPath("/")%><BR>

    当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%><BR>

    <%

       String path=application.getRealPath(request.getRequestURI());

       String dir=new File(path).getParent();

       out.println("当前JSP文件所在目录的物理路径"+dir);

    %>

    </body>

    </html>

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    String virtPath = request.getServletPath();//虚拟路径

    String realPath = request.getRealPath(virtPath);//物理路径

    JSP中获得当前应用的相对路径和绝对路径

    根目录所对应的绝对路径:request.getRequestURI()

    文件的绝对路径    :application.getRealPath(request.getRequestURI());

    当前web应用的绝对路径 :application.getRealPath("/");

    取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()

    Servlet中获得当前应用的相对路径和绝对路径

    根目录所对应的绝对路径:request.getServletPath();

    文件的绝对路径   :request.getSession().getServletContext().getRealPath

    (request.getRequestURI())  

    当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");

        (ServletContext对象获得几种方式:

          Javax.servlet.http.HttpSession.getServletContext()

          Javax.servlet.jsp.PageContext.getServletContext()

          Javax.servlet.ServletConfig.getServletContext()

        )

    Java 的Class中获得相对路径,绝对路径的方法

    单独的Java类中获得绝对路径

    根据Java.io.File的Doc文挡,可知:

    默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。

    一下程序获得执行类的当前路径

    package org.cheng.file;

    import Java.io.File;

    public class FileTest {

       public static void main(String[] args) throws Exception {     

    System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));    

    System.out.println(FileTest.class.getClassLoader().getResource(""));       

      System.out.println(ClassLoader.getSystemResource(""));       

    System.out.println(FileTest.class.getResource(""));       

    System.out.println(FileTest.class.getResource("/")); //Class文件所在路径

    System.out.println(new File("/").getAbsolutePath());       

    System.out.println(System.getProperty("user.dir"));   

    }

    }

    服务器中的Java类获得当前路径(来自网络)

    (1).Weblogic

    WebApplication的系统文件根目录是你的weblogic安装所在根目录。

    例如:如果你的weblogic安装在c:\bea\weblogic700.....

    那么,你的文件根路径就是c:\.

    所以,有两种方式能够让你访问你的服务器端的文件:

    a.使用绝对路径:

    比如将你的参数文件放在c:\yourconfig\yourconf.properties,

    直接使用 new FileInputStream("yourconfig/yourconf.properties");

    b.使用相对路径:

    相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放

    在yourwebapp\yourconfig\yourconf.properties,

    这样使用:

    new FileInputStream("./yourconfig/yourconf.properties");

    这两种方式均可,自己选择。

    (2).Tomcat

    在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin

    (3).Resin

    不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET

    的路径为根.比如用新建文件法测试File f = new File("a.htm");

    这个a.htm在resin的安装目录下

    (4).如何读相对路径哪?

    在Java文件中getResource或getResourceAsStream均可

    例:getClass().getResourceAsStream(filePath);//filePath可以是"/filename",这里的/代表web

    发布根路径下WEB-INF/classes

    默认使用该方法的路径是:WEB-INF/classes。已经在Tomcat中测试。

  • 相关阅读:
    白雪公主与七个爷爷——冲刺日志(第二天)
    vba实践
    微信小程序scroll-view不起作用,bindscrolltolower事件无响应
    git bash相关命令
    hexo博客中命令备忘(markdown语法)
    微信聊天记录生成词云图(等日后有机会补充完整)
    微信小程序解决存在图片太大而有效显示区域较小--阿里云oss产生缩略图
    css样式基础库--global.css(持续更新中~)
    Chrome控制台常用技巧
    css基础-动画效果
  • 原文地址:https://www.cnblogs.com/demonspider/p/2720744.html
Copyright © 2020-2023  润新知