重写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>test@hotmail.com</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 : test@hotmail.com
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(),因此输出的为test@hotmail.com。此时并没有对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>test@hotmail.com</param-value>

</init-param>

</servlet>

 

<servlet-mapping>

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

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

</servlet-mapping>

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值