我附上我的两个程序: 1.test.java import java.util.Date; import java.text.DateFormat; import java.io.Serializable;public class HitCountBean implements Serializable { private boolean b; private int hitCount; private String serverStart; private Date date = new Date(); public String getStartTime() { return serverStart; } public int getHits() { return hitCount; } public void setNewSession(boolean b) { b = b; if (b) { hitCount++; } }
public HitCountBean() { hitCount = 0; serverStart = DateFormat.getDateInstance(DateFormat.LONG).format(date); } } 2.test.jsp <%@ page errorPage = "uh-oh.jsp" %> <%@ page import = "HitCountBean" %> <jsp:useBean id="counter" scope="application" class="HitCountBean" /> <jsp:setProperty name="counter" property="newSession" value="<%= session.isNew() %>" /> <html> <head><title>Hit Counter</title> <style type="text/css"> </style> </head> <body> <h1>Hit Counter</h1> <p>This page has had <jsp:getProperty name="counter" property="hits" /> user sessions since the server started on <jsp:getProperty name="counter" property="startTime" /> </p> </body> </html>
o ,知道问题在哪里了,原来定义HitCountBean 的文件必须是HitCountBean.java,呵呵,谢谢了
HitCounter.java编译好后
放到\doc\WEB-INF\classes下一定好使有无package?
1.test.java
import java.util.Date;
import java.text.DateFormat;
import java.io.Serializable;public class HitCountBean implements Serializable
{
private boolean b;
private int hitCount;
private String serverStart;
private Date date = new Date(); public String getStartTime()
{
return serverStart;
} public int getHits()
{
return hitCount;
} public void setNewSession(boolean b)
{
b = b;
if (b)
{ hitCount++; }
}
public HitCountBean()
{
hitCount = 0;
serverStart = DateFormat.getDateInstance(DateFormat.LONG).format(date);
}
}
2.test.jsp
<%@ page errorPage = "uh-oh.jsp" %>
<%@ page import = "HitCountBean" %>
<jsp:useBean id="counter" scope="application" class="HitCountBean" />
<jsp:setProperty name="counter" property="newSession"
value="<%= session.isNew() %>" />
<html>
<head><title>Hit Counter</title>
<style type="text/css">
</style>
</head>
<body>
<h1>Hit Counter</h1>
<p>This page has had <jsp:getProperty name="counter" property="hits" />
user sessions since the server started on <jsp:getProperty name="counter"
property="startTime" />
</p>
</body>
</html>
建立的函数的名字用“counter”
(HitCountBean.java)
我测了,没有问题