aspose file tools*
The moose likes Servlets and the fly likes How can a non-servlet find application root and best way to access a servlets data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can a non-servlet find application root and best way to access a servlets data?" Watch "How can a non-servlet find application root and best way to access a servlets data?" New topic
Author

How can a non-servlet find application root and best way to access a servlets data?

Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
In a general servlet class after deployment, you can access the application root as servletContext.getRealPath("/") to access any of the properties files in the "WEB-INF" directory or anywhere else in the /webapp-root/ directory.

How can we get the equivalent servletContext.getRealPath("/") in a non-servlet where you are interested to access a property file located in "/webapp-root/WEB-INF" or probably a text file stored in a static folder pages in the /webapp-root/pages/ folder.

How do you access application root static pages or WEB-INF jars or class directory paths from a non-servlet class

ANOTHER PROBLEM:

Subject: How do we share data between a servlet and a non-servlet when you want to access data that changes in time?

I am looking for a solution to update a .jsp page (kind of progress bar) which shows current progress in a huge simulation when users have to wait for longer periods and the way to do it was have the jsp page refresh often and the page data update by a bean containing the properties file details. The main servlet class continously running on the server regularly updates the properties file.

I am currently using a properties file, which is updated by property IO methods load() and store() which is then accessed by a non-servlet class to read and perform specific actions. Potential problems could be when reading and writing into properties file by two different sources at the same time. Another bigger problem is this works only for one user. This cannot be used by multi-user as the properties file cannot be instantiated/shared unlike a servlet.

The problem is that there cannot be a session or application scoped variable that can be shared between a servlet and non-servlet easily and hence a lack of easier way of communicating with each other.

Any other good solutions to such problems??

Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Originally posted by Rama Krishna:
How do you access application root static pages or WEB-INF jars or class directory paths from a non-servlet class

I don't. I extract any data I need at the servlet layer and pass it onto the UI-agnostic classes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
"How do you access application root static pages or WEB-INF jars or class directory paths from a non-servlet class"

So you are saying a non-servlet class cannot access a static page or WEB-INF folder property file at all?

And, if you had a non-servlet that is running continously and extensively and you wanted to know the progress by keeping track of some of the variables. How would you do that? All I wanted was to have some variables inside a servlet or non-servlet to be saved somewhere, into a properties file in my case and then another class accesses this property file to do any thing else, like display progress in a jsp file. But a property file is such a menace and unfortunately, none of the servlet session scoped variables or rather any other variable cannot be accessed by a non-servlet class.

There must be someway to share data as in servlets share session or application scoped data between other servlets.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Originally posted by Rama Krishna:
So you are saying a non-servlet class cannot access a static page or WEB-INF folder property file at all?
I'm not saying that at all. I'm saying that it's unwise to define non-UI classes that are servlet-aware. Rather make the classes UI-agnostic. Pass them a file path or resource handle, not the servlet context or any other UI-specific element.
[ February 18, 2008: Message edited by: Bear Bibeault ]
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
I am more looking to a way to do it.

"And, if you had a non-servlet that is running continously and extensively and you wanted to know the progress by keeping track of some of the variables. How would you do that? All I wanted was to have some variables inside a servlet or non-servlet to be saved somewhere, into a properties file in my case and then another class accesses this property file to do any thing else, like display progress in a jsp file. But a property file is such a menace and unfortunately, none of the servlet session scoped variables or rather any other variable cannot be accessed by a non-servlet class.

There must be someway to share data as in servlets share session or application scoped data between other servlets."
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

It might help to know why you don't want to use application scope. You haven;t really explained why this needs to be handled at levels lower than the UI.
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Had I used application scope, could you please tell me briefly how I may access and change these variables in a non-servlet class?

In my case, I have a huge list of variables that I am storing in a properties file and updating separately while this one scheduler servlet is continuously running in the background. There must be some way to access a servlet's inner variables dynamically outside in a non-servlet class or jsp page when the servlet is still running!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

When you instanciate your non-servlet classes, pass them a handle to your properties object or whatever it is that they need to access.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

We seem to be going around in circles. UI classes can access it directly, non-UI classes have the data passed to it.
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
I have not got a solution yet. That is exactly the way I am doing my servlet and non-servlet class interaction. The thing is that in my case, I have a huge simulation, essentially a hibernate (slow) related database queries run to get data from various tables for various cases and store into some place. I am storing the progress in a properties file by using load and store methods on the properties file. However, I have too many parameters in the properties file that are being updated regularly. Another servlet just reads the properties file and updates the jsp page content regularly working as a progress bar.

Question 1) There should be some other way as in threading or as in session scoped variables that get shared between multiple classes. I am not sure how I can share data in the application scope or rather, could someone explain me how I can save data into the application scope and access it from the non-servlet please.

Question 2) Another question is how do I get the context root or application root the way we get using getServlet().servletContext().getRealPath("/") and how do we access the static pages in the \webapp\ folder and the "\WEB-INF\" folder properties files from a non-servlet class?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rama Krishna:


Question 1) There should be some other way as in threading or as in session scoped variables that get shared between multiple classes. I am not sure how I can share data in the application scope or rather, could someone explain me how I can save data into the application scope and access it from the non-servlet please.

Question 2) Another question is how do I get the context root or application root the way we get using getServlet().servletContext().getRealPath("/") and how do we access the static pages in the \webapp\ folder and the "\WEB-INF\" folder properties files from a non-servlet class?



For both questions, the answer is the same and has been said more than once in this thread.

To access shared objects from non-servlet classes, you will want to pass references to the objects when they are instanciated.

For example, in your app, you said that you are storing data in a properties object. If you instanciate that object from a contextListener and bind it to context scope, you will be able to pass it to any non-servlet classes from your servlets.



Now your non-servlet object has a reference to your properties object.
Furthermore, your non-servlet object doesn't depend on being run from with a servlet container. It could just as easily be embedded in a command line or Swing app.
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
If I understand correctly, what you are really doing here is that you are setting a nonservlet object property in a servlet. You can then pass this object or run any of the methods of the nonServletObject on these "myProps" file. Fine, but my scenario is quite different.



First, I do not know how to instantiate an object using the contextlistener and set it to context scope so that any part of the application may access. I am not sure how this will allow me to solve the problem of being able to access any file in a general utility class just the way we do it in a servlet using servletContext()?

Anyways, I still think my problem is quite different, you are passing the object directly from the servlet and you suggest to pass these objects from the servlet by setting the objects property, but in case of a stand alone utility class how can I access any other file. Anyways, I found a solution of saving the context root, probably a listener class will do this job when the server is started, somewhere in a property file (as session scoped variables cannot be read directly by a non-servlet) and then reading the context root from that location?

The other problem:
Let's say a servlet or a non-servlet is continuously running and I want another servlet to continously update any changes in the variables in that servlet.

Non-Servlet A
(continously running
& saving to some prop --> (servlet B reads--> display continously
file ) prop file regularly) on a jsp file

I currently have a non-servlet A saving into a properties file some variables at regular intervals and then using the servlet B to read this properties file to update the jsp page. Can we share these variables in any other way??

Or is there a way to do this in a way like this.

Servlet A (continously running
& saving to some bean/object -->(servlet B reads--> display continously
TempSessObj in session) session object TempSessObj) on a jsp file

If so, how do I START the servlet A and then still have the user jsp connected or updated by Servlet B only? I can probably store the session variables continously in the Servlet A which can then be accessed by Servlet B regularly when it is updated.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What do you mean by stand alone utility class?
How are you instaciating objects from this class?
If it's from a servlet or another object that is part of the servlet spec (like a contextListener), then you have access to whatever that object needs (path info, etc).
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Gosh, its taking me so long to just explain my problem..

Non-Servlet A
(continously running
& saving to some prop --> (servlet B reads--> display continously
file) prop file regularly) on a jsp file

This non-servlet is what I was calling a stand-alone utility class which continuously runs itself using the timerTask utility.

There are no objects to instantiate here. Some jsp page calls this method first to start this method which just runs continuously and I control it by some thread and time properties.

Now tell me..
if we can share these variables or data from this non-servlet with the servlet in any other way??

Or is there a way to do this in a way like this.

Servlet A (continously running
& saving to some bean/object -->(servlet B reads--> display continously
TempSessObj in session) session object TempSessObj) on a jsp file

If so, how do I START the servlet A and then still have the user jsp connected or updated by Servlet B only? I can probably store the session variables continously in the Servlet A which can then be accessed by Servlet B regularly when it is updated.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rama Krishna:

Non-Servlet A
(continously running
& saving to some prop --> (servlet B reads--> display continously
file) prop file regularly) on a jsp file

This non-servlet is what I was calling a stand-alone utility class which continuously runs itself using the timerTask utility.


How are you starting (instanciating) Non-Servlet A?
If the answer is "from the timerTask utility", then what is starting the timerTask utility?
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110

How are you starting (instanciating) Non-Servlet A?
If the answer is "from the timerTask utility", then what is starting the timerTask utility?


Here's how I did it.

I have a jsp which first calls a action servlet method and I set a session attribute saying that this method should not be run any more. This action class method invokes a scheduler method which actually contains timer tasks

Timer timer1 = new Timer();
timer1.scheduleAtFixedRate(someTask, currDate, repititionPeriod);
the class someTask extends TimerTask

So although this method returns safely and there is a thread of this non-servlet running safely in the application just probably the way servlet context listener does the job of initializing some object when server starts (not sure how to use this).

Once this non-servlet runs it can continue to run until you shutdown the server. So I have the jsp interact with the user to take new inputs and based on those inputs saved into a properties file the non-servlet is able to use read these fields for the next run and so on.. In fact I update the properties file data sometimes by the non-servlet which is again read by the a servlet to update the jsp.

Somehow, I got it working and I am able to show non-servlet progress in my JSP. But the code sucks as it works only for a single user as a stand alone. Obviously I cannot instantiate the hard properties file for multi user.

Now its your turn..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

OK, so you have a subclass of TimerTask that needs access to a properties file (or vice versa).

Create a Properties attribute in your someTask class with a setter method.


Now, from your container managed object (jsp, servlet, listener), instanciate the task and pass it an instance of your properties object before scheduling it.

[ February 21, 2008: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rama Krishna:

Somehow, I got it working and I am able to show non-servlet progress in my JSP. But the code sucks as it works only for a single user as a stand alone. Obviously I cannot instantiate the hard properties file for multi user.


It sounds like you might be a little over your head with this.
You will most likely run into concurrency issues unless you do a lot of synchronizing; which could result in a very slow app.

You might want to step back a bit and study the fundamentals of Servlet and JSP development. Once you do, you may find that there is a much simpler way of accomplishing whatever it is that you're trying to do.
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Well I already know that there could be issues when more than one user sets up the scheduling. Right now, it is only for a single user, so there are no issues involved.

Anyways why don't you please suggest me what those other simple ways are. I can always read books but I wanted some help by posting to this group along with comments!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

We really need to step back and re-evaluate from first principles.

Firstly, ignore the fact that there are Servlets, and design a system that manages the update-and-cache responsibilities. Personally I'd throw out the requirements for Properties files and just keep it in memory.

We tend to use OSCache. Define your cache settings (can be done its own properties file) and then run with it.

Now we need to plug in your 'data loading', and then finally make it work in a servlet container.

Part Two: the DataLoading



Part One: The OSCache.
We choose blocking calls, LRU cache, hold for one minute and a max of 1000 values at a time. Cached entries will be keyed against the key you pass in.
 
jQuery in Action, 2nd edition
 
subject: How can a non-servlet find application root and best way to access a servlets data?
 
Similar Threads
Webspere 345 exam questions
test 286 q&a pass 88% (can someone tell me where I was wrong)
Creating a class within JSP to be shared among pages
IBM Exam 157 - Questions and Answers (Sample Exam)
ICE Test 286 solved 89 %