Using JavaBeans into JSP

I have a simple example for using Beans in JSP and the problem is that i get this error: org.apache.jasper.JasperException: /BeanCounter.jsp(8,0) The value for the useBean class attribute Counter is invalid.

The program's ideea is to count how many times i've accesed the bean

This is the code: (i'm using Apache Tomcat 5.5)

My bean (Counter):

import java.io.Serializable;

public class Counter implements Serializable{

int count = 0;

public Counter() {

}

public int getCount() {

count++;

return this.count;

}

public void setCount(int count) {

this.count = count;

}

and the code for BeanCounter.jsp :

<HTML>

<HEAD>

<TITLE>JSP Bean Example</TITLE>

</HEAD>

<BODY>

<%@ page language="java" %>

<jsp:useBean id="counter" scope="session" class="Counter" /> //i guess that here is the problem but i don't have a package or anything else

<jsp:setProperty name="counter" property="count" param="count" />

<%

out.println("Count from scriptlet code : "

+ counter.getCount() + "<BR>");

%>

<!-- Get the bean's count property, -->

<!-- using the jsp:getProperty action. -->

Count from jsp:getProperty :

<jsp:getProperty name="counter" property="count" />

</BODY>

</HTML>

The Counter.class is in WEB-INF/classes

I just cannot understand why it doesn't work.Please help :) Thanks in advance

[1611 byte] By [Traiana] at [2007-10-3 1:11:01]
# 1

So put your class in a package, and it should work fine.

package mypackage;

import java.io.Serializable;

import java.io.Serializable;

public class Counter implements Serializable {

int count = 0;

public Counter() {

}

public int getCount() {

count++;

return this.count;

}

public void setCount(int count) {

this.count = count;

}

}

and in your JSP:

<jsp:useBean id="counter" scope="session" class="mypackage.Counter" />

The class then moves to be WEB-INF/classes/mypackage/Counter.class

Also, your original post was missing a closing '}' on the Counter class.

evnafetsa at 2007-7-14 18:07:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
It worked,.. **** packages :P THANK YOU very very much
Traiana at 2007-7-14 18:07:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...