如何从env-entry获取参数?
从env-entry获取:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-to-win:用env-entry存放的参数,本个web应用中的任何servlet,jsp都可以获得。和context-param相比,env-entry存放的参数不止为String类型。下面的comp是component的意思。
例:1.4.1
ServletHello1.java:
package com;
import java.io.IOException;
import java.io.InputStream;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Context ctx;
boolean isReady = false;
try {
ctx = new InitialContext();
Boolean b = (Boolean) ctx.lookup("java:comp/env/isReady");
isReady = b.booleanValue();
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println("" + isReady);
}
}
web.xml中,在任何“servlet标签对儿”的外面加入(因为env-entry参数不属于任何特定的servlet,属于整个web应用):
<env-entry>
<env-entry-name>isReady</env-entry-name>
<env-entry-type>java.lang.Boolean</env-entry-type>
<env-entry-value>true</env-entry-value>
</env-entry>
运行Servlet后,console中输出结果:
true