Thursday, August 25, 2011

How to get project name under Tomcat server

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetPathTest {
    private String webApps    = "webapps";
    private String webINF    = "WEB-INF";

    public String getClassPath(){
        return this.getClass().getClassLoader().getResource(File.separator).getPath();
    }
   
    public String getProjectName(){
        String classPath = getClassPath();
        Pattern pattern = Pattern.compile(webApps+".*"+webINF);
        Matcher matcher = pattern.matcher(classPath);
        String projectName = null ;
        while (matcher.find()) {
            projectName = matcher.group();
            projectName = projectName.substring(webApps.length()+1,projectName.length()-(webINF.length()+1));
        }
        if (projectName==null) return "null";
        return projectName;
    }
}

No comments:

Post a Comment