获取tomcat中web.xml中定义的环境变量(env-entry)

时间:2022-11-02 14:21:04
import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 获取tomcat下应用的web.xml文件中的env-entry
* 本例获取的原定义为:
* <env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>F:/developer/solr-tomcat/solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

* @author larry
*
*/
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -796123516638912971L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {

Context c = new InitialContext();
String home = (String)c.lookup("java:comp/env/solr/home");
System.out.println("solr-home:" + home);

} catch (NamingException e) {
e.printStackTrace();
}

}

}