两种方法:
1.String pro1name = System.getProperty("user.dir");
2.File sysFilePath = new File("./");
String myFilePath = sysFilePath.getAbsolutePath();
myFilePath = myFilePath.substring(0, myFilePath.length() - 1);得到的结果都是tomcat的路径,那么我怎么才能获取到工程的名称呀?
1.String pro1name = System.getProperty("user.dir");
2.File sysFilePath = new File("./");
String myFilePath = sysFilePath.getAbsolutePath();
myFilePath = myFilePath.substring(0, myFilePath.length() - 1);得到的结果都是tomcat的路径,那么我怎么才能获取到工程的名称呀?
web程序用servletContext.getContextPath();
request.getRealPath("/") 在Jsp里面会获取web根目录
String projectName = request.getContextPath();执行到这里会出错呀error:org.seasar.framework.exception.ClassNotFoundRuntimeException
你或者做个Servlet,在Servlet启动时,将工程名设置给你的变量就行了啊
package test;
import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SetContextPath extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
service(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
service(req, resp);
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String contentPath = request.getContextPath();
String realPath = request.getRealPath("/"); //TODO: 调用你的类设置 如
//Sysinfo.setContentPath(contentPath);
//Sysinfo.setRealPath(realPath); super.service(request, response);
}
}
web.xml
<servlet>
<servlet-name>SetContextPath</servlet-name>
<servlet-class>test.SetContextPath</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SetContextPath extends HttpServlet
{
public static A a; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
service(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
service(req, resp);
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String contentPath = request.getContextPath();
String realPath = request.getRealPath("/"); a.setProjectName(contentPath); super.service(request, response);
}
@Override
public void init() throws ServletException
{
a=new A();
super.init();
}
}
先取到工程一长条字符,然后分割取我想要的。下面的解决方法可能会有问题,还希望大家指出
String projectPath = this.getClass().getResource("/").toString();
String[] projectName = projectPath.split("/");
int index=0;
for(int i=0;i<projectName.length;i++){
if(projectName[i].equals("src")) {
index = i-1;
}
}
System.out.println(projectName[index]);