-2

I am working on struts 2.0 . I am designing a web application. I am using Jasper Report in my application. I want to access the *.jrxml files in my action class. I don't want to give hard coded path to the files. So to get the path dynamically I googled it and got the solution that I can get the path using getRealPath() method. But I found two implementation of doing this:

  1. Using HttpSession to get object of ServletContext and using the getRealPath() method of the ServletContext object. Like this:

    HttpSession session = request.getSession();
    String realPath = session.getServletContext().getRealPath("/");
    
  2. The second approach to do it directly using the static method getServletContext() of ServletActionContext. And then we can get the real path of the application using the getRealPath() method. Like this:

    String realPath = ServletActionContext.getServletContext().getRealPath("/");

Please tell me, is there any difference between the above two and also please tell me whether there is any other way to get the path?

Vicky
  • 871
  • 9
  • 16
  • Related: http://stackoverflow.com/questions/12160639/what-does-mean-in-the-function-servletcontext-getrealpath and http://stackoverflow.com/questions/14843615/servlet-failing-to-write-uploaded-image-to-disk – BalusC Jun 04 '13 at 13:49
  • Why don't you put your files in the webapp? – Aleksandr M Aug 16 '13 at 17:36
  • @AleksandrM These files are in my webapp. But There are three places where we deploy our application. localhost to develop and test the application, then demos server of our company and the live server. And if I give the hard coded path of the `/WebContent/reports/` location then I have to change it every time when I deploy it to the server accordingly. And I want a way, so that I do not need to change it every time. – Vicky Aug 16 '13 at 18:06
  • See http://stackoverflow.com/q/18181296/1700321. – Aleksandr M Aug 16 '13 at 18:07

1 Answers1

0

Neither is "better", really, and I'd argue that neither is particularly good, either.

I might try getting the context path in an initialization servlet and stick it into the application context, then make your action(s) ApplicationAware and retrieve the value from the map.

This has the added benefit of aiding testability and removing the static references in the action.

That said, I see zero reason to go through the extra mechanics of your first approach: it adds a lot of noise for no perceivable benefit; I'm not even sure why it wuld be considered.

I'd also be a little wary of tying your actions to a path like this unless there's a real need, what's the specific use? In general you shouldn't need to access intra-app resources by their path.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I am using JasperReports in my application, the path is required to access the jasper files in action class and also i am using the path string as field, so that I can access the images required for the report. All I am doing is to avoid giving the static path. – Vicky Jun 05 '13 at 05:35