Failed to access file in WEB-INF folder from servlet

I had a problem accessing the files in the WebContent folder (in this case, I need to access the WEB-INF folder)

Here I am trying to access the WEB-INF folder.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String dbPath = "/WEB-INF/musicDb.db"; String xmlPath = "/WEB-INF/musicDb.xml"; ServletContext context = null; context = getServletContext(); String test = context.getRealPath(xmlPath); File f = new File(test); if (f.exists()) { } } 

I have musicDb.xml ready in the WEB-INF folder, however, when debugging, I always get this path instead

 {my local project path}/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/9321A1/WEB-INF/musicDb.xml 

enter image description here

I suspect this is because the WebContent folder is not included in the build process. This may be wrong.

Almost every textbook / sentence / article I have found so far points me to this approach using

getServletContext (). GetRealPath ()

which doesn't seem to work in my case

Any help would be appreciated.

Tang

+1
java servlets
source share
1 answer

I suggest you move the file to / WEB-INF / classes or pack it in a jar and place this jar in WEB-INF / lib. Download the file as a resource using any of the getResourceAsSteam methods

See here: Recommendations for storing and accessing resource files in a java web application and the correct answer here: Reading web pages Application resources

+1
source share

All Articles