代码如下
在tomcat的webapp目录下新建一个文件夹myCounter,Counter.jsp存放在myCounter 的WEB-INF层,Counter.class打包为beans也存放在WEB-INF层
package beans;
public class Counter{
private long counter;
public long getCounter(){
return counter;
}
public void setCounter(long counter){
this.counter = counter + 1;
}
}
Counter.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
<jsp:useBean id="counter007" class="beans.Counter" scope="application"></jsp:useBean>
<html>
<head><title>javaBean计数器</title></head>
<body>
<%
if(session.isNew())
{
long temp = counter007.getCounter();
counter007.setCounter(temp);
}
%>
<font size="2"><strong>本页展示的是利用javaBean实现的计数器</strong></br>
欢迎光临,你是本网站的第
<jsp:getProperty name="counter007" property="counter" />
个访客</br>
</font>
</body>
</html>
报错内容如下
org.apache.jasper.JasperException: /Counter.jsp(2,0) The value for the useBean class attribute beans.Counter is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1200)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1155)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2388)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2394)
org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2338)
org.apache.jasper.compiler.Generator.generate(Generator.java:3374)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:210)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
------解决方案--------------------
在JSP文件中应该使用<%@ import="beans.Counter">引入你所要使用的JavaBean