how to get the path to the WEB-INF directory

hello everyone,

I placed a custom config file in the WEB-INF directory of my vaadin web app…

How can I get the path to the WEB-INF directory (and of course the config file) at runtime

Thanks in advance

Peter

Hi Peter,

you can construct the path to a file inside the webapp folder by using WebApplicationContext, which you can get by calling getContext of your Vaadin application class instance:

WebApplicationContext context = (WebApplicationContext)getContext();
File webinfFolder = new File ( context.getHttpSession().getServletContext().getRealPath("/WEB-INF") );

Alternatively, you could want to read your config file at app startup - in this case just register your ServletContextListener and in contextInitialized(…) callback method you’ll find the ServletContext as a incoming parameter

thanks for your answer

I find it very usefull

Halo Dmitri Livotov,

I am newbie for vaadin.

would you explain me more about WebApplicationContext, I have got error when wrote this line

“com.vaadin.terminal.gwt.server.WebApplicationContext cont = (com.vaadin.terminal.gwt.server.WebApplicationContext cont) getContext();”

The error is " the method getContext() is underfined

Thank’s

It’s a method in the Application class which you extend in your application.

Hi Salman,

as Marko mentioned, please make sure you’re calling getContext from your application class which extends Vaadin’s Application.

@Marko Grönroos and Dmitri Livotov,

my class extends with panel’s class, that why I got error and it’s running as well when I changed my class extended with Application class.

Thank for your help.

[size=5]
[b]
[1]

[/b]
[/size]Could you please explain where/how to call the getContext method?
This is my main Vaadin application class where I call getContext as you explained - but I get the error:

The method getContext() is undefined for the type MyprojectUI

[code]
public class MyprojectUI extends UI {
@Override
protected void init(VaadinRequest request) {

WebApplicationContext context = (WebApplicationContext)getContext();
File webinfFolder = new File (context.getHttpSession()
.getServletContext().getRealPath(“/WEB-INF”) );

    final VerticalLayout layout = new VerticalLayout();
    setContent(layout);
    layout.setSizeFull();
    Layout layout_component = new Layout();
    layout.addComponent(layout_component);
}

}
[/code]
What am I doing wrong?

[size=5]
[b]
[2]

[/b]
[/size] Also, I have tried the following to retrieve the WEB-INF path:

String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath(); System.out.println(basepath); However, this gives me the path:

C:\Users\Username\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BAProject

Instead of:

C:\Users\Username\workspace\MyProject


So why is that?

[size=5]
[b]
[3]

[/b]
[/size] third question…

File pfad = new File(""); System.out.println(pfad.getAbsolutePath()); Doing that will print out:
C:\Eclipse

But shouldn’t it also referr to the application directory (i.e. User/Workspace…) ??

[1]
, [2]

  • if you just want to get the absolute path to WEB-INF then [2]
    is fine
    the reason you get that weird path is because Eclipse deploys it as a webapp in a temp directory under .netadata (and not directly at the same place) and so when you’re running it you get that path

[3]
not sure. I think Eclipse uses its own root dir as the root for abosolute paths