Reputation: 3751
I am trying to get the file paths of various JSP files inside my webapp
folder after deploying the project in Tomcat. Here is what my directory structure looks like:
-project
|-top
||-java
|||-com
||||-...
|||-filter
||||-MyFilter.java
||-webapp
|||-jsp
||||-MyJSP.jsp
So when the code hits my filter, what would the path be to MyJSP.jsp
inside of the doFilter()
method? I am trying to edit the contents of the file, so I need to be able to do read
and write
operations.
When I do request.getServletPath()
, it shows the path: /jsp/MyJSP.jsp
. However, when I try to read using the following code:
String str = "";
BufferedReader br = new BufferedReader(new FileReader("/jsp/MyJSP.jsp"));
while ((str = br.readLine()) != null) {
System.out.println(str );
}
br.close();
I get a FileNotFoundException
. What am I doing wrong? Is that not the actual path of the jsp file after it is deployed in Tomcat?
Also, I noticed some other questions saying put the files in the WEB-INF
folder, however, there is nothing in that folder except for web.xml
and I am not allowed to put anything in there.
Any help would be appreciated!
Upvotes: 0
Views: 179
Reputation: 5122
You may have to use request.getServletContext().getRealPath("/jsp/MyJSP.jsp")
.
BufferedReader br = new BufferedReader(new FileReader(request.getServletContext().getRealPath("/jsp/MyJSP.jsp")));
Upvotes: 1