【发布时间】:2018-12-05 02:27:13
【问题描述】:
我必须计算页面的访问次数,但是当计数为奇数时,我不应该 打印计数,我必须从自定义标签中执行此操作。我无法从自定义标签中调用我的字段计数..
这是我的代码:
索引jsp文件
<%
Integer count = (Integer)application.getAttribute("numberOfVisits");
if (count == null || count == 0)
{
out.println("Welcome!");
count = 1;
}
else
{
out.println("Welcome back");
count++;
}
application.setAttribute("numberOfVisits", count);
%>
<%@ taglib uri="/WEB-INF/mytags.tld" prefix="c" %>
<c:counter></c:counter>
<%=count%>
自定义标签类:
public int doEndTag() throws JspException{
try
{
int count = application.getAttribute("numberOfVisits") // wrong
if (count % 2 != 0) return EVAL_PAGE;
}
catch(Exception e)
{
e.printStackTrace();
}
return SKIP_PAGE;
}
}
【问题讨论】:
标签: jsp servlets servletcontextlistener custom-tags application-scope