permaculture playing cards
The moose likes Servlets and the fly likes help with passing context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help with passing context" Watch "help with passing context" New topic

help with passing context

Jamil Zakaria

Joined: Jul 20, 2006
Posts: 7

basically I a sevlet context listener which i use to runs non servlet classes at a particular time. the code is as follows

however when I run these classes (that by the way create text files that need saving on the server hence the need for context path) I want to be able to pass a string with the context path to this classes.

I know I can place the following in contextinitialized to get context,

ServletContext context = getServletContext();
String path2 = context.getRealPath("/")+"Grouped-URL-Files";

I then want to pass the string to the run() method which inturn is passed to each class executed.

However I have not been able to do that yet I tried various things however I keep getting compling problems relating to static and non static and/or null values for the string.

Can anyone tell me how to do it, is it actually possible or is there another way to tell the classes the context path?

Please help

thanks in advance

[ August 09, 2006: Message edited by: Bear Bibeault ]
[ August 10, 2006: Message edited by: Jamil Zakaria ]
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I'm not looking deep into it yet, but why don't you pass the path (not a pun) to RunClasses' constructor ?

[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

If you are going to post more than single line of code, please us the UBB code tags to preserve the formatting.

Please read this.

You can go back and edit your post to fix the code formatting.

[Asking smart questions] [About Bear] [Books by Bear]
Jamil Zakaria

Joined: Jul 20, 2006
Posts: 7
Could you please provide an example of how to run it through the constructor thanks.
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
As Satou said, you can pass the required info in the constructor. RunClasses will now have a private instance variable for storing the path info.

Pass the desired path in the servlet context listener like this

Hope it helps.
Jamil Zakaria

Joined: Jul 20, 2006
Posts: 7
thanks, the code worked perfectly!!!
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
Thats Great!

Oops... I forgot to pass the path here to the constructor

I agree. Here's the link:
subject: help with passing context
jQuery in Action, 3rd edition