重写jspInit()对ServletContext进行方法时,注意配置servlet-mapping

做了个小实验,对email.jsp的jspInit()方法进行重写,首先通过ServletConfig获取email,并请其存储在ServletContext中,最后对存储的attribute进行输出。具体配置如下。

web.xml配置:

<servlet>

<servlet-name>email</servlet-name>

<jsp-file>/email.jsp</jsp-file>

<init-param>

<param-name>email</param-name>

<param-value>[email protected]</param-value>

</init-param>

</servlet>

email.jsp:

<%!

public void jspInit() {

ServletConfig config = getServletConfig();

String email = config.getInitParameter("email");

System.out.println("jspInit : "email);

ServletContext ctx = getServletContext();

ctx.setAttribute("email", email);

}

%>

<%= application.getAttribute("email") %>

启动tomcat 结果为:

 写道
2012-7-9 16:54:44 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.27
jspInit : [email protected]
2012-7-9 16:54:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\tomcat\webapps\docs
2012-7-9 16:54:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\tomcat\webapps\examples
2012-7-9 16:54:44 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2012-7-9 16:54:44 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2012-7-9 16:54:44 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@22ce00')
2012-7-9 16:54:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\tomcat\webapps\host-manager
2012-7-9 16:54:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\tomcat\webapps\manager
2012-7-9 16:54:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\tomcat\webapps\ROOT
2012-7-9 16:54:44 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
2012-7-9 16:54:44 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
2012-7-9 16:54:44 org.apache.catalina.startup.Catalina start
信息: Server startup in 571 ms
jspInit :null

注意其中2行红字部分:

第1行:在容器启动时会对jsp进行转换操作,并调用其jspInit(),因此输出的为[email protected]。此时并没有对email.jsp 进行访问,因此<%= application.getAttribute("email") %>还没有执行。

第2行:对页面(email.jsp) 进行访问,输出的结果为jspInit : null。这里非常奇怪,为什么jspInit会执行2次?而且结果为null。我们知道jspInit()只在初始化的时候执行,而且容器中只有这么一个实例,也就是说最多只会执行一次而已。

通过eclipse debug一下,发现这里分别实例化了2个ServletContext对象。。。于是想到一定是某些配置不对,导致容器的实例化是出现一些问题。

最终发现,一定要配置相应的<servlet-mapping>,而且url-pattern必须为相应email.jsp,修改如下:

<servlet>

<servlet-name>email</servlet-name>

<jsp-file>/email.jsp</jsp-file>

<init-param>

<param-name>email</param-name>

<param-value>[email protected]</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>email</servlet-name>

<url-pattern>/email.jsp</url-pattern>

</servlet-mapping>

目前还不清楚,容器具体的实现细节。

猜你喜欢

转载自bboyuan.iteye.com/blog/1583120