-1

I have been using this "ServletContext.getRealPath("")" till Tomcat8 and I got the return string path ending with a slash.

For Example ServletContext.getRealPath("")+"resources" will return /home/company/eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Project_Title/resources

But when I tried with Tomcat 9.0 I got path without a slash

For Example ServletContext.getRealPath("")+"resources" will return /home/company/eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Project_Titleresources

Slash after 'Project_Title' is missing. This exception only occurs after deployment(.WAR). There is no issue in local.

Hope somebody can help me.

Thank you.

  • You can avoid all that by *not* concatenating path strings, and instead using `Path.get(base, extension)`. – M. Prokhorov Aug 27 '19 at 10:21
  • Thanks for your response. How to get 'base'? – Suhail Moideen Aug 27 '19 at 10:25
  • It isnt 'missing'. It isn't supposed to be there. What you get is a valid directory name. *Not* a URL. You can use it to compose filenames in the ordinary way. – user207421 Aug 27 '19 at 10:32
  • @SuhailMoideen, you should follow WebResources API and use paths that start with "/", as it expects. See [ServletContext::getContextPath](https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getContextPath()). – M. Prokhorov Aug 27 '19 at 10:33
  • `ServletContext.getRealPath("")+"resources"` could have been written as `new File(ServletContext.getRealPath(""), "resources")`, but really you should have written it far more simply as `ServletContext.getRealPath("resources")`. – user207421 Aug 27 '19 at 10:52

2 Answers2

0

I Fixed the issue.

The issue was, I didn't put '/' before "resources".

ServletContext.getRealPath("")+"/resources"
-1

you need to pass web-inf in to getrealpath().if you pass nothing the it will return you full absolut path of you project.

application=getServletContext(); application.getRealPath("WEB-INF"); //retrun null application.getRealPath("/WEB-INF"); //RETRUN absoluteServletContext `