Root path of a webapp

  • Thread starter Sebastian Stein
  • Start date
S

Sebastian Stein

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
 
R

Real Gagnon

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.
 
R

Real Gagnon

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

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

Bye.
 
S

Sebastian Stein

Real Gagnon said:
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
 
L

Luke Webber

Sebastian said:
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.

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
 
J

John C. Bollinger

Sebastian said:
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.

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.
 
S

Sebastian Stein

Sebastian Stein said:
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.

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
 
H

Hiran Chaudhuri

Sebastian Stein said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top