Java examples for Servlet JSP:Servlet
get Web App Context Path
//package com.java2s; import java.net.URL; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; System.out.println(getContextPath(clazz)); }//from w w w .ja v a 2 s .c o m static String getContextPath(Class<?> clazz) { ClassLoader loader = clazz.getClassLoader(); if (loader == null) return null; URL url = loader.getResource("/"); if (url != null) { String[] elements = url.toString().split("/"); for (int i = elements.length - 1; i > 0; --i) { // URLs look like this: file:/.../ImageServer/WEB-INF/classes/ // And we want that part that's just before WEB-INF if ("WEB-INF".equals(elements[i])) return elements[i - 1]; } } return null; } }