两种方法:
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.   

    user.dir是java应用程序的地址,当然会是tomcat了,如果你用weblogic,还会是weblogic的路径。
    web程序用servletContext.getContextPath();
      

  2.   

    request.getContextPath() 获取web工程的名称
    request.getRealPath("/") 在Jsp里面会获取web根目录
      

  3.   

    private HttpServletRequest request;
    String projectName = request.getContextPath();执行到这里会出错呀error:org.seasar.framework.exception.ClassNotFoundRuntimeException
      

  4.   

    因为我的前端部是jsp,而是html
      

  5.   

    你HTML 怎么玩啊?也不是一定在JSP里面,只要可以取到request地方就可以获取,比如 Filter,Servlet
      

  6.   

    不是我想html,项目的前端就是html,我也没有办法呀,一个叫seasar的框架,就是这样的,在类里就没办法获取工程的名字了么?
      

  7.   

    获取,你直接把 HTML 后缀改成JSP得了。有时候也需要适当的变通。如果再不行,你就嵌个隐藏的Iframe,用Iframe请求JSP页面好了.不过,我想知道你获取了工程名称要哪里使用?
      

  8.   

    肯定是不能改成jsp了,我在想,在类里就取不了工程名么?
      

  9.   

    取得了啊。怎么会取不了呢。只要实现Filter,Servlet就可以了啊。
    你或者做个Servlet,在Servlet启动时,将工程名设置给你的变量就行了啊
      

  10.   

    朋友,我真是不懂Filter,呵呵,我现在去google查资料,实在不行就得麻烦你了,呵呵
      

  11.   


    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>
      

  12.   

    首先非常感谢maer56,呵呵
      

  13.   

    但是,我还是有些不太懂,这个filter是用来获取工程名的,我还有一个A类,我想在A类当中,有个String projectName;我想让这个projectName赋值成工程名,那么怎么才能和你帮我写的那个Fileter联系在一起呀,不好意思,再次打扰了
      

  14.   

    看错了,是servlet,不是fileter,但是上面的问题还是不变的,呵呵
      

  15.   

    lz概念好像不太清楚,连request,servletcontext都拿不到,没法取。即使框架也应该可以有入口。
      

  16.   

    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
    {
        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();
        }
    }
      

  17.   

    谢谢maer56先,呵呵我是这么做的
    先取到工程一长条字符,然后分割取我想要的。下面的解决方法可能会有问题,还希望大家指出
    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]);