Announcement Announcement Module
Collapse
No announcement yet.
Getting a JSP to recognize a cookie Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting a JSP to recognize a cookie

    I have the following in a JSP. What I'm trying to do is show different content depending on the presence of a cookie, which is being sent from my custom tag, according to Firebug. That cookie is not being recognized.

    Code:
    <c:set var="foundCookiePoll1" value="false" scope="page" />
    <c:set var="poll1Yes" value="0" scope="page" />
    <c:set var="poll1No" value="0" scope="page" />
    		<tr>
    			<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>
    					<c:otherwise>
    						<form:form action="poll1" modelAttribute="poll1" method="post">
    							<table>
    								<tr>
    									<td><b><i>Poll #1</i></b></td>
    								</tr>
    								<tr>
    									<td>Would you like to have a 30-year reunion in 2016?<br>
    									</td>
    								</tr>
    								<tr>
    									<td><form:radiobutton path="vote" value="yes" />Yes <form:radiobutton
    											path="vote" value="no" />No</td>
    								</tr>
    								<tr>
    									<td><input name="submit" type="submit"
    										value="Vote Poll #1" align="left" /></td>
    								</tr>
    							</table>
    						</form:form>
    					</c:otherwise>
    				</c:choose></td>
    		</tr>
    Here is the content of the custom tag.
    Code:
    	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 where the cookie gets set and sent.
    Code:
    	@RequestMapping(value = "/poll1", method = RequestMethod.POST)
    	public String processPoll1(@RequestParam String vote,
    			HttpServletResponse response, Model model) {
    		poll1DAO.tallyVote(vote);
    		response.addCookie(new Cookie("poll1", "voted"));
    		return "redirect:/polls";
    	}

  • #2
    Problem solved by putting this scriptlet in my JSP.

    Code:
    	<%
    		Cookie[] cookies = request.getCookies();
    		if (cookies != null) {
    			for (int i = 0; i < cookies.length; i++) {
    				if (cookies[i].getName().equals("poll1")) {
    					%> <c:set var="foundCookiePoll1" value="true" /> <%
    				}
    				
    				if (cookies[i].getName().equals("poll2")) {
    					%> <c:set var="foundCookiePoll2" value="true" /> <%
    				}
    				
    				if (cookies[i].getName().equals("poll3")) {
    					%> <c:set var="foundCookiePoll3" value="true" /> <%
    				}
    			}
    		}
    	 %>

    Comment

    Working...
    X