Being in a rush for a release, I had to integrate a portion of a JSP made with custom tags with another part, that shows some informations with scriptlest.
To make the two work together, I exposed an object from the tag handler doStartTag()
to the request scope, retrieved that object in the scriptlet, and then removed it at the end of the tag handler execution flow, inside doEndTag()
.
Some code should clarify my situation.
Here's the relevant part of the tag handler:
public int doStartTag() throws JspException {
// ...
// obj is a private member
pageContext.setAttribute("productFromTag", obj, PageContext.REQUEST_SCOPE);
return EVAL_BODY_INCLUDE;
}
// ...
public int doEndTag() throws JspException {
pageContext.removeAttribute("productFromTag", PageContext.REQUEST_SCOPE);
return EVAL_PAGE;
}
And this is the portion of the JSP where the object is retrieved and processed:
<pre:product><!-- this adds the Product instance to the request scope -->
<div class="item">
<!-- other HTML -->
<p class="productName"><prod:name></prod:name></p>
<%
Product p = (Product) request.getAttribute("productFromTag");
// Use p
%>
<!-- other HTML -->
</div>
</pre:product>
Everything works fine after the first deploy, but if I replace the JSP (say after an edit), the removeAttribute()
method throws a NullPointerException
:
Caused by: java.lang.NullPointerException
at org.apache.jasper.runtime.PageContextImpl.doRemoveAttribute(PageContextImpl.java:398)
at org.apache.jasper.runtime.PageContextImpl.removeAttribute(PageContextImpl.java:387)
at xx.xxxxx.xxxx.ProductTag.release(ProductTag.java:89)
at org.apache.jasper.runtime.TagHandlerPool.release(TagHandlerPool.java:165)
at org.apache.jsp.modelli.camediResponsive.catalogo.schedaProdotto_jsp._jspDestroy(schedaProdotto_jsp.java:200)
at org.apache.jasper.runtime.HttpJspBase.destroy(HttpJspBase.java:60)
at org.apache.jasper.servlet.JspServletWrapper.destroy(JspServletWrapper.java:478)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:166)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
... 83 more
By reading removeAttribute()
javadoc, no exception should be thrown.
Am I misreading the docs? Am I doing something wrong?
I usually replace (i.e. redeploy) JSP's, both when editing inside Eclipse or in running production applications, but I've neved faced this situation.
Of course I've already added a catch()
around the method invocation, but I'm puzzled about why this is happening.