您的位置:首页 > 编程语言 > Java开发

自己动手写struts笔记1

2016-03-12 20:53 330 查看
package com.gd.mvc.util;

import java.io.IOException;

import java.net.URL;

import java.security.CodeSource;

import java.security.ProtectionDomain;

/**

* 这是一个公共类 用于获取web环境的路径 名称等信息

* @author lx

*

*/

public class GdWebPath {

private String webName;

private String webPhysicsPath;

private String systemName;

private String systemPath;

/**

* 该名称返回的是web名称,如:myApp

* @return String

*/

public String getWebName(){

return this.webName;

}

/**

* 该路径指的是web环境的物理路径,如:D:\eclipse\workspace\myApp

* @return

*/

public String getWebPhysicsPath(){

return this.webPhysicsPath;

}

public void getPath()throws IOException{

Class cls2=this.getClass();

ProtectionDomain pDomain=cls2.getProtectionDomain();

CodeSource cSource=pDomain.getCodeSource();

URL loc=cSource.getLocation();//file:/D:/eclipse/workspace/myApp/WEB-INF/classes/com/gd/web/webPath.class

webPhysicsPath=loc.toString().substring(loc.toString().indexOf("/")+1,loc.toString().indexOf("WEB-INF")-1);

System.out.println("webPhysicsPath "+webPhysicsPath);

int num=webPhysicsPath.lastIndexOf("/");

webName=webPhysicsPath.substring(num+1,webPhysicsPath.length());

System.out.println("webName: "+webName);

}

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

// GdWebPath gdw=new GdWebPath();

// gdw.getPath();

// }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: