• Post Reply Bookmark Topic Watch Topic
  • New Topic

to find the workspace path at runtime to write the file

 
Gnanam Meenakshi Sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a Struts project deployed in tomcat webapps. User Sign in details are configured in a xml file and are validated by reading the xml file..The xml file is in the path WEB-INF/classes/com/files/user.xml...
when a new user has to be added his/her details has to be written in the same user.xml file..I read the the file using
getClass().getClassLoader().getResourceAsStream("com/files/user.xml");

To write the file using filewriter if I have to give the full path without hardcoding it.
For eg.
D:/SDE3.0/workspace/project/WebContent/WEB-INF/classes/com/files/user.xml
when I tried to get this path URL using getClass().getClassLoader().getResource("com/files/user.xml") , i get
C:/Program Files/SDE3.0/tomcat-5.5.16/webapps/Request/WEB-INF/classes/com/files/user.xml

Please suggest me how to solve this issue or is there way to find the workspace path at runtime so that I can avoid hardcoding of this path.
Thanks in advance
Regards,
Gnanam
 
Bhavik patel
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...well I am new in scwcd...
can you jus tell me why do you need the file in your development structure to be muniplated by your web application....?

dont you think the worspace folder structure will not exist where you are going to deploy your application...?

waiting for your reply...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gnanam Meenakshi Sundaram:

To write the file using filewriter if I have to give the full path without hardcoding it.
For eg.
D:/SDE3.0/workspace/project/WebContent/WEB-INF/classes/com/files/user.xml
when I tried to get this path URL using getClass().getClassLoader().getResource("com/files/user.xml") , i get
C:/Program Files/SDE3.0/tomcat-5.5.16/webapps/Request/WEB-INF/classes/com/files/user.xml


Sounds to me like getResource is working correctly. It can only find resources on the classpath and your XML file is deployed with your application. If you want to change the copy of the file that is in your project directory, you will not be able to use getResourceAsStream to load it. Not to mention that you will need to redeploy the app to see changes to the file. If you persist your changes in the deployment directory you'll overwrite the changes with each redeployment.
It would probably be a good idea to move that file outside your web project and configure its location via a configuration parameter or move this functionality to a standard service (i.e. a database).
 
Gnanam Meenakshi Sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for your quick replies
It would probably be a good idea to move that file outside your web project and configure its location via a configuration parameter


I am new to this technology.
Could you please reply me how to configure the location of a file outside webapp via a configuration parameter.
 
Gnanam Meenakshi Sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can you jus tell me why do you need the file in your development structure to be muniplated by your web application....?

dont you think the worspace folder structure will not exist where you are going to deploy your application...?
]


Thanks for your reply.
After deploying this is what happened.
when I used getClass().getClassLoader().getResourceAsStream() to read the file I expected it to load from C:/Program Files/SDE3.0/tomcat-5.5.16/webapps/Request/WEB-INF/classes/com/files/user.xml.But it loads the file from my workspace directory. ie..D:/SDE3.0/workspace/project/WebContent/WEB-INF/classes/com/files/user.xml.So writing in this same location can keep the file in sync.
 
Gnanam Meenakshi Sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can you jus tell me why do you need the file in your development structure to be muniplated by your web application....?

dont you think the worspace folder structure will not exist where you are going to deploy your application...?
]


Thanks for your reply.
After deploying this is what happened.
when I used getClass().getClassLoader().getResourceAsStream() to read the file I expected it to load from C:/Program Files/SDE3.0/tomcat-5.5.16/webapps/Request/WEB-INF/classes/com/files/user.xml.But it loads the file from my workspace directory. ie..D:/SDE3.0/workspace/project/WebContent/WEB-INF/classes/com/files/user.xml.So writing in this same location can keep the file in sync.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gnanam Meenakshi Sundaram:
. . .But it loads the file from my workspace directory. . .


When I put your two posts together, it appears that getResourceAsStream() is returning the workspace file but getResource() returns the path to the deployed file. That just doesn't seem possible. How do you know that the file loaded by getResourceAsStream() is the workspace file?
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gnanam Meenakshi Sundaram:

Could you please reply me how to configure the location of a file outside webapp via a configuration parameter.


I usually use a Properties file to encapsulate all the configuration information for an application. If this path is all you need, you could probably get away with putting the path in web.xml as a init-param to a servlet which you can then access via javax.servlet.ServletConfig.
 
Gnanam Meenakshi Sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:

it appears that getResourceAsStream() is returning the workspace file but getResource() returns the path to the deployed file. That just doesn't seem possible. How do you know that the file loaded by getResourceAsStream() is the workspace file?


you are absolutely right.Actually getResouceasStream didnt load it from workspace.I have mistaken it like that because I made a change in the file in the worspace location.Then redeployed my app in webapps.Then used get getResouceasStream to check it.The change I made reflected in my output which made me misunderstood the concepts. What happenned was ,as I redeployed ,the change was also made in webapps and getResouceasStream has loaded from it.I think now my doubt got cleared.Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!