The Artima Developer Community
Sponsored Link

Java Answers Forum
application-level variables in servlet on Domino

15 replies on 2 pages. Most recent reply: Oct 13, 2003 10:16 PM by Nandita

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 15 replies on 2 pages [ 1 2 | » ]
Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

application-level variables in servlet on Domino Posted: Aug 19, 2003 3:35 AM
Reply to this message Reply
Advertisement
I am using Domino as my Web server (no WebSphere or anything), so my application is composed of Domino forms and servlets (no JSPs)

I am trying to create and retrieve an application-level variable in my servlet. This is the code:

ServletContext application = getServletConfig().getServletContext();
String somethingToStore = "test";
Object o = application.setAttribute("appVar", (Object)somethingToStore);
String somethingToRetreive = (String) application.getAttribute("appVar");

However, I find that I get an error for the third line, stating:
"Method setAttribute(java.lang.String, java.lang.Object) not found in interface javax.servlet.ServletContext."

How do I set the variable?

(I am able to set a static variable in a class, import the class, and use that value as an application-scoped variable, but would prefer to use ServletContext instead of importing the class everywhere.)


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: application-level variables in servlet on Domino Posted: Aug 20, 2003 1:52 AM
Reply to this message Reply
Can't you use a session?

Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Aug 20, 2003 3:20 AM
Reply to this message Reply
Yes, I can use a session variable - I am already doing that for actions that depend on a specific user's role, etc.

However, when I need a variable that is common to the ENTIRE application (and does not change from user to user) - such as the database name, path, stylesheet path, etc. , it does not make sense to keep resetting it for every user, and creating so many instances, when I need just one instance.

Of course, I could carry the value across - post it from one servlet to the next each time - but it is too painful to pass it along everywhere. I want to be able to set it just once, and retrieve it anywhere.

Shekhar

Posts: 11
Nickname: shekhar4u
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Aug 22, 2003 5:22 AM
Reply to this message Reply
I think you can declare all the variables which you wanted to use for application level, in properties file and use getInitParameter method of ServletConfig to retrieve anytime u need those variables. This properties file is accessible from any servlet through out the application.

vinod

Posts: 1
Nickname: vinodjava
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Aug 22, 2003 11:22 AM
Reply to this message Reply
dear Nandita,

setAttribute method's return type is "void",Plz check ur code once.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: application-level variables in servlet on Domino Posted: Aug 23, 2003 7:57 AM
Reply to this message Reply
The document for setAttribute is as follows:

void setAttribute(java.lang.String name, java.lang.Object object)

change the line from:
Object o = application.setAttribute("appVar", (Object)somethingToStore);


to

application.setAttribute("appVar", somethingToStore);

You don't have to cast a String to Object

Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Sep 26, 2003 5:17 AM
Reply to this message Reply
I tried this, but it doesn't seem to work either:
application.setAttribute("appVar", somethingToStore);

It still gives me the same message:

"Method setAttribute(java.lang.String, java.lang.Object) not found in interface javax.servlet.ServletContext."

Shekhar

Posts: 11
Nickname: shekhar4u
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Sep 27, 2003 12:50 AM
Reply to this message Reply
2 reasons for that error:
1. either setAttribute method not there in servletcontext of domino web server (may be someother method used in place of setAttribute...check)
2. or no such overloaded setAttribute method in servletcontext.
check out the api of ur domino webserver

Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Sep 28, 2003 4:30 AM
Reply to this message Reply
Yes; that is the problem - Domino supports an older API, in which this method was probably not defined. That is why I am looking for a workaround to this!

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Sep 29, 2003 5:56 PM
Reply to this message Reply
What version of the servlet spec is supported?
At least as far back as Servlet 2.1, ServletContext has had a method public void setAttribute(String key, Object value).
http://java.sun.com/products/servlet/2.1/api/javax.servlet.ServletContext.html#setAttribute(javax.servlet.String,%20java.lang.Object)

Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Oct 7, 2003 9:34 PM
Reply to this message Reply
I have been unsuccessful with the application.setAttribute method. However, Shekhar's (shekhar4u) suggestion of getInitParameter method works.

I set the variable like this in the properties file
servlet.ServActPreModify.initArgs = databaseName=ProjTrack.nsf, stylesheetPath=../ProjTrack.nsf/style_all.css

Here, ServActPreModify is the name of my servlet, while databaseName and stylesheetPath are the names of my "global" variables.

My question is: How can I make it generic enough, so that all servlets can refer to it? I tried servlet.*.initArgs, but that did not work.

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: application-level variables in servlet on Domino Posted: Oct 8, 2003 10:12 PM
Reply to this message Reply
Try this:

servlets.default.initArgs=[name]=[value],[name]=[value],...

The name-value pairs defined this way are common to all servlets. This one works for JServ, don't know about Domino though, but you can try.

Nandita

Posts: 10
Nickname: nanditasr
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Oct 9, 2003 2:30 AM
Reply to this message Reply
I have already tried that; it doesn't work with Domino.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Oct 10, 2003 10:02 AM
Reply to this message Reply
What about a base servlet with a protected static variable?
public abstract class BaseHttpServlet extends javax.servlet.http.HttpServlet {
    protected static java.util.HashMap globalVariables = new java.util.HashMap();
}

This won't work in a cluster, as each JVM would have its own copy of "globalVariables".

From what I've read, Domino only supports JSDK 2.0 (Servlet API 2.0), in which the ServletContext is read-only.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: application-level variables in servlet on Domino Posted: Oct 10, 2003 10:07 AM
Reply to this message Reply
If you use this, you should synchronize access to the static variable:
public abstract class BaseHttpServlet extends javax.servlet.http.HttpServlet {
    protected static java.util.HashMap globalVariables = new java.util.HashMap();
    public void addGlobalAttribute(Object key, Object value) {
        synchronized (globalVariables) {
            globalVariables.put(key, value);
        }
    }
    public Object getGlobalAttribute(Object key) {
        Object value = null;
        synchronized (globalVariables) {
            value = globalVariables.get(key);
        }
        return value;
    }
}

Flat View: This topic has 15 replies on 2 pages [ 1  2 | » ]
Topic: How to create a frame that has a specific graphic interface? Previous Topic   Next Topic Topic: need to draw a city map to do Uniform search

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use