• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access Singleton class method thru jsp useBean

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