Question

I have a custom tag that is not being run by my JSP. The tag is s supposed to run a query on my database and return those values to my JSP, but I get zeros in my page when I know for a fact there are non zero values in the database. I've run the application in debug mode, and the tag is not being called for some reason, but I seem to be getting a NullPointerException for some reason, even though the tag exists. Here is the relevant portion of my JSP. This part only appears when a cookie is present.

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<td><myTag1:poll1 /> <c:choose>
        <c:when test="${foundCookiePoll1 == true}">
            <table>
                <tr>
                    <td><b><i>Poll #1 -- </i></b>Would you like to have a
                        30-year reunion in 2016?<br></td>
                </tr>
                <tr>
                    <td><b>Yes</b></td>
                    <td>&nbsp;&ndash;&nbsp;<c:out value='${poll1Yes}' /><br />                      </td>
                </tr>
                <tr>
                    <td><b>No</b></td>
                    <td>&nbsp;&ndash;&nbsp;<c:out value='${poll1No}' /><br />                       </td>
                </tr>
            </table>
        </c:when>

Here is my tag.

public void doTag(HttpServletRequest request)
        throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    HttpSession session = request.getSession(true);
    ServletContext servletContext = session.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);
    Poll1DAO poll1DAO = (Poll1DAO) wac.getBean("poll1DAO");

    pageContext.setAttribute("foundCookiePoll1", cookieFound());
    if (cookieFound()) {
        HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
        pageContext.setAttribute("poll1Yes", (int) poll1Votes.get("yes"));
        pageContext.setAttribute("poll1No", (int) poll1Votes.get("no"));
    }
}

private boolean cookieFound() {
    PageContext pageContext = (PageContext) getJspContext();
    HttpServletRequest request = (HttpServletRequest) pageContext
            .getRequest();
    Cookie[] cookies = request.getCookies();

    if (cookies == null) {
        return false;
    }

    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("poll1")) {
            return true;
        }
    }

    return false;
}

Here is my tld.xml for this tag.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>Poll1Tag</short-name>
    <uri>poll1</uri>
    <tag>
        <name>poll1</name>
        <tag-class>com.tags.Poll1Tag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>
Was it helpful?

Solution

This may not be the best way to do this, but in the controller method for the page do this.

    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("poll1")) {
                HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
                model.addAttribute("poll1Yes", (int) poll1Votes.get("yes"));
                model.addAttribute("poll1No", (int) poll1Votes.get("no"));
            }
        }
    }

In the JSP, refer to the variables like this.

<%= poll1Yes %>

Get the variables like this.

int poll1Yes = (Integer) request.getAttribute("poll1Yes");
int poll1No = (Integer) request.getAttribute("poll1No");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top