当前位置:编程学习 > JAVA >>

web.xml里面可以直接获得这个项目的路径吗。

还有其他配置文件里面可以直接获得项目路径吗。?怎么获得……求知道
web.xml文件的截图

(注:ask.longcom.com就是这个项目配的域名。。。想在web.xml里面获取到。。而不是这样写死。。有什么方法吗?) web.xml 域名 --------------------编程问答-------------------- 你是要程序中获取这个值还是??
web.xml就是你配置了,然后程序才会使用到这些配置。。你反过来咋弄,要么就用程序写文件。
--------------------编程问答--------------------

public class DelegatingServletProxy extends GenericServlet {

private static final long serialVersionUID = 5659702440376079848L;
private String targetBean;
private Servlet proxy;

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
proxy.service(req, res);
}

@Override
public void init() throws ServletException {
this.targetBean = getServletName();
getServletBean();
proxy.init(getServletConfig());
}

private void getServletBean() {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet) wac.getBean(targetBean);
}

}
给你参考下.这是获取web.xml配置的
可以getServlet的name
--------------------编程问答-------------------- 应该你要首先写入到xml 中,然后再启动服务读取xml文件。  --------------------编程问答-------------------- 我是想在web.xml里面不直接把这个param-value里的值写死,而是随着项目域名变可以跟着变的。。。

我就是想表达这个意思。不知道说清楚没有。。。。。 --------------------编程问答-------------------- PS:我是菜鸟……各位大神请说的稍微详细点。。不然我真看不懂。。。 --------------------编程问答-------------------- 这个是在项目初始化的时候写死的吧,灵活点的就是写在 properties 文件里,但那也是项目启动后无法修改的。
如果你这域名是要在项目启动后动态改变,建议用其他方法吧。 --------------------编程问答--------------------
引用 6 楼 defonds 的回复:
这个是在项目初始化的时候写死的吧,灵活点的就是写在 properties 文件里,但那也是项目启动后无法修改的。
如果你这域名是要在项目启动后动态改变,建议用其他方法吧。

其他方法是什么方法大神 --------------------编程问答-------------------- 这个是不行了,因为这个文件是配置文件,是你配置好了,在程序里面去用, --------------------编程问答-------------------- 你是想让 servlet 动态拿到这个值,对吧 --------------------编程问答--------------------
public class ServletTest extends HttpServlet {
private ServletConfig  config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value=this.getServletConfig().getInitParameter("serverName");
//或者用下面的
String value2=config.getInitParameter("serverName");
System.out.println(value+","+value2);

//得到所有的
Enumeration e= this.getInitParameterNames();
while(e.hasMoreElements()){
String key=(String) e.nextElement();
String v=this.getServletConfig().getInitParameter(key);
System.out.println(key+"="+v);
}



}


@Override
public void init(ServletConfig config) throws ServletException {
this.config=config;
super.init(config);

}


}
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,