简体   繁体   中英

Alternative for request.getSession().getServletContext().getRealPath()

I am migrating Java web application from Tomcat 6 to Tomcat 8. In the application ,on a button click, pdf gets created first in 'Workspace.metadata.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\Project' folder if it does not exists and then opened in a new window.

This functionality works fine Tomcat 6 and 7 . But in Tomcat 8 ,pdf gets created but its not opening on first click(getting 404 error) . On second or third click it's getting displayed.

Below code is used to get the path of the pdf location:

String path = request.getSession().getServletContext().getRealPath("//MyPdf.pdf");

Hard coding of pdf path is working fine in Tomcat 8 as well . I am not able to understand what is the issue , suggest some alternative for the above code.

I can see 2 slashes before your file name.

String path = request.getSession().getServletContext().getRealPath("//MyPdf.pdf");

Below code should work.

String path = request.getSession().getServletContext().getRealPath("/MyPdf.pdf");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM