wood burning stoves 2.0*
The moose likes JSP and the fly likes access  Singleton class method  thru jsp useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "access  Singleton class method  thru jsp useBean" Watch "access  Singleton class method  thru jsp useBean" New topic
Author

access Singleton class method thru jsp useBean

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I want to access Singleton classes's method thru jsp's useBean..
How can i do that?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

As you would any Java class.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You could use an intermediary bean.



This way, you have an object that complies with all of the JavaBean requirements that you can scope however you like.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I would like to access without an intermediary bean.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

UseBean requires an object that meets all of the Javabean criteria.
One of those requirements is a zero-argument constructor.
Since singleton objects have private constructors, you'll need to make sure that something else instanciates and binds the singleton to the desired scope before the JSP that uses it is ever called. A ContextListener comes to mind.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
More explantion about my problem:

It's my Singleton class.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.HashMap;

public class Singleton {

private static Singleton instance = new Singleton();

static HashMap hmap = new HashMap();

private Singleton() {
}

public static Singleton getInstance() {
return instance;
}


public HashMap selectFromBackend() {
try {

for (int j = 0; j < size; j++) {
// getting the values from the db and add it to the HashMap
hmap.put(new Integer(j), new Integer(value));

}

} catch (Exception e) {
System.out.println(e.getMessage());
}
return hmap;
}

}


thru jsp's useBean i want to access Singletons classes HashMap values
test.jsp
--------

jsp:useBean id="id" class="trial.Singleton"/>

<%
HashMap map = id.getInstance().selectFromBackend();
%>

i am using Tomcat5. When i try to access this http://localhost:8080/Test/test.jsp , i got the following exception

org.apache.jasper.JasperException: /test.jsp The value for the useBean class attribute trial.Singleton is invalid.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

UseBean is for people who want to get get scriptlets out of their JSPs.
Since you're using scriptlets anyway, why not just instanciate the Singleton from your JSP without the useBean tag?

<%
trial.Singleton id = trial.Singleton.getInstance();
HashMap map = id.selectFromBackend();
%>

UseBean is for use with beans. Your singleton class is not a bean.
 
Consider Paul's rocket mass heater.
 
subject: access Singleton class method thru jsp useBean
 
Similar Threads
How to use Java Application in jsp:useBean
Implementing the Singleton Pattern in EJB
Whats a Singleton alternative ?
Refresh a Singleton Object values
Difference between useBean and importing java classes