Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access Singleton class method thru jsp useBean

 
kri shan
Ranch Hand
Posts: 1464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to access Singleton classes's method thru jsp's useBean..
How can i do that?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you would any Java class.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kri shan
Ranch Hand
Posts: 1464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to access without an intermediary bean.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic