关于获取工程名称

Gong_WP 2010-01-28 10:07:21
两种方法:
1.String pro1name = System.getProperty("user.dir");
2.File sysFilePath = new File("./");
String myFilePath = sysFilePath.getAbsolutePath();
myFilePath = myFilePath.substring(0, myFilePath.length() - 1);

得到的结果都是tomcat的路径,那么我怎么才能获取到工程的名称呀?
...全文
928 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
但是,我还是有些不太懂,这个filter是用来获取工程名的,我还有一个A类,我想在A类当中,有个String projectName;我想让这个projectName赋值成工程名,那么怎么才能和你帮我写的那个Fileter联系在一起呀,不好意思,再次打扰了
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
首先非常感谢maer56,呵呵
maer56 2010-01-28
  • 打赏
  • 举报
回复

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>

Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
朋友,我真是不懂Filter,呵呵,我现在去google查资料,实在不行就得麻烦你了,呵呵
maer56 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 gong_wp 的回复:]
肯定是不能改成jsp了,我在想,在类里就取不了工程名么?
[/Quote]
取得了啊。怎么会取不了呢。只要实现Filter,Servlet就可以了啊。
你或者做个Servlet,在Servlet启动时,将工程名设置给你的变量就行了啊
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
肯定是不能改成jsp了,我在想,在类里就取不了工程名么?
maer56 2010-01-28
  • 打赏
  • 举报
回复
获取,你直接把 HTML 后缀改成JSP得了。有时候也需要适当的变通。

如果再不行,你就嵌个隐藏的Iframe,用Iframe请求JSP页面好了.

不过,我想知道你获取了工程名称要哪里使用?
maer56 2010-01-28
  • 打赏
  • 举报
回复
写个过滤器,用过滤器去取。
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
不是我想html,项目的前端就是html,我也没有办法呀,一个叫seasar的框架,就是这样的,在类里就没办法获取工程的名字了么?
iso_wangjian 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yys79 的回复:]
user.dir是java应用程序的地址,当然会是tomcat了,如果你用weblogic,还会是weblogic的路径。
web程序用servletContext.getContextPath();
[/Quote] q
maer56 2010-01-28
  • 打赏
  • 举报
回复
你HTML 怎么玩啊?

也不是一定在JSP里面,只要可以取到request地方就可以获取,比如 Filter,Servlet
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
因为我的前端部是jsp,而是html
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
我想在类中取工程名,呵呵
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
private HttpServletRequest request;
String projectName = request.getContextPath();执行到这里会出错呀

error:org.seasar.framework.exception.ClassNotFoundRuntimeException
whereusejava 2010-01-28
  • 打赏
  • 举报
回复
楼上正解
maer56 2010-01-28
  • 打赏
  • 举报
回复
request.getContextPath() 获取web工程的名称
request.getRealPath("/") 在Jsp里面会获取web根目录

loveunittesting 2010-01-28
  • 打赏
  • 举报
回复
user.dir是java应用程序的地址,当然会是tomcat了,如果你用weblogic,还会是weblogic的路径。
web程序用servletContext.getContextPath();
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
以上还是有错误的,公司破框架,所以没发现出来,大家仅供参考吧,呵呵
Gong_WP 2010-01-28
  • 打赏
  • 举报
回复
谢谢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]);
maer56 2010-01-28
  • 打赏
  • 举报
回复
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();
}
}
加载更多回复(3)

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧