Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Root path of a webapp

Thread view: 
Sebastian Stein - 08 Dec 2005 19:13 GMT
Sorry for this question, I think it is often asked, but I still can't find an
answer.

I have a Java webapp (using Tomcat) with a JSP and a class extending Action.
The user selects an option in the jsp. The input is forwarded to the class
using Struts. It is now the task of the class to store the setting in the
WEB-INF/ directory.

How does the class get the root path of the webapp? Or the path of the WEB-INF
dir? I guess there must be a way, but I can't figure out how.

Sebastian
Real Gagnon - 08 Dec 2005 21:42 GMT
> I have a Java webapp (using Tomcat) with a JSP and a class extending
> Action. The user selects an option in the jsp. The input is forwarded
> to the class using Struts. It is now the task of the class to store
> the setting in the WEB-INF/ directory.

Try  application.getRealPath("WEB_INF");  in your JSP.

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Real Gagnon - 08 Dec 2005 21:45 GMT
> Try  application.getRealPath("WEB_INF");  in your JSP.

Oups ... application.getRealPath("/WEB-INF");

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Sebastian Stein - 08 Dec 2005 21:47 GMT
>> I have a Java webapp (using Tomcat) with a JSP and a class extending
>> Action. The user selects an option in the jsp. The input is forwarded
>> to the class using Struts. It is now the task of the class to store
>> the setting in the WEB-INF/ directory.
>
> Try  application.getRealPath("WEB_INF");  in your JSP.

Yes, this may work in the JSP and from the JSP I can forward this property to a
ActionForm or to the session. But I thought there must be a way to directly
get the directory in the Action class or even in a bean class.

Sebastian
Luke Webber - 08 Dec 2005 23:06 GMT
>>>I have a Java webapp (using Tomcat) with a JSP and a class extending
>>>Action. The user selects an option in the jsp. The input is forwarded
[quoted text clipped - 6 lines]
> ActionForm or to the session. But I thought there must be a way to directly
> get the directory in the Action class or even in a bean class.

Not directly, but you could try one of several things. You could keep a
static or singleton class that keeps your configuration info, and store
the directory path there when it's first initialised from your servlet
or JSP. Or you could use System.setProperty("user.dir", <path>) to
achive the same result, then just call System.getProperty("user.dir") to
retrieve it.

HTH,
Luke
John C. Bollinger - 09 Dec 2005 00:46 GMT
>>>I have a Java webapp (using Tomcat) with a JSP and a class extending
>>>Action. The user selects an option in the jsp. The input is forwarded
[quoted text clipped - 6 lines]
> ActionForm or to the session. But I thought there must be a way to directly
> get the directory in the Action class or even in a bean class.

In JSP the automatic variable "application" is a reference to the JSP's
ServletContext.  I'm not much familiar with the details of Struts, but
if you can get a hold of the ServletContext through some Struts
mechanism then you can do the same thing with it that you can do in JSP
scriptlet code.

Signature

John Bollinger
jobollin@indiana.edu

Hiran Chaudhuri - 09 Dec 2005 18:57 GMT
>> Try  application.getRealPath("WEB_INF");  in your JSP.
>
[quoted text clipped - 3 lines]
> directly
> get the directory in the Action class or even in a bean class.

The struts action class has the request, right?
Then do an request.getSession().getServletContext().getRealPath(...)

For business logic you do not want to be dependent on the servlet api. So in
any case your action should get the path and pass it on to your business
logic.

Hiran
Sebastian Stein - 09 Dec 2005 15:26 GMT
> I have a Java webapp (using Tomcat) with a JSP and a class extending Action.
> The user selects an option in the jsp. The input is forwarded to the class
[quoted text clipped - 3 lines]
> How does the class get the root path of the webapp? Or the path of the WEB-INF
> dir? I guess there must be a way, but I can't figure out how.

Just for the record that next one googling for this can find a solution:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
String path = loader.getResource("file").toURI().getPath();

This only works, if "file" is in the classpath of the app. So if you know the
file you are looking for is somewhere in WEB-INF/classes/ then this is the way
to go.

Sebastian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.