AJF AJF - 2 years ago 67
Java Question

How to create one instance of a class available to entire servlet

I have a JSP application with a plain java class of Login.java and a servlet with a call to a procedure named loginList in the doGet method. The loginList procedure needs to create a list of logins using a java class named OAVDbUtil with a method "getLoginsList". But Eclipse does not seem to recognise the "getLoginsList" procedure and when I enter it the massage of "eclipse cannot make a static reference to the non-static method" is given. But everything looks okay as I have not stated the getLoginsList as static. I think there is a way to create an instance of the OAVDbUtil to avoid having to create multiple instances of New OAVdbUtil objects but can someone tell me how to do this please and NOT get the message of "eclipse cannot make a static reference to the non-static method"?

Here is some code and a screen dump

Screen dump of servlet

enter image description here

Here is the code for the OAVDbUtil

public OAVDbUtil(DataSource theDataSource) {
dataSource = theDataSource;

public List<Login> getLoginsList() throws Exception {

List<Login> loginList = new ArrayList<Login>();

Answer Source

You need to provide an instance of OAVDbUtil to the servlet, and then call getLoginsList() on that object.

A servlet might construct such an object in its initialization method, or it could be injected into the servlet by a container.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download