GWT - RPC - get servlet absolute path to make dir

坚强是说给别人听的谎言 提交于 2020-01-14 09:08:02

问题


I tried to read a file with my "RemoteServiceServlet" but the thing is

  • I want to create a dir which path is related to servlet like

absolute_servlet_path/myPackage/

But the problem is...

code like

ServletContext servletContext = this.getServletContext();
String pathContext = servletContext.getRealPath("");

... gives not "C:/..." but "/" only so the code cannot be used with java.io.File object.

So my question is how can I use java.io.File with RemoteServiceServlet ?


回答1:


OK... I had to keep digging...

Still I was looking for a way of servlet relative path but an absolute one; and now again I tried to use the context but this time I modified my code in this direction...

ServletContext servletContext = this.getServletContext();
String pathContext = servletContext.getRealPath("/WEB-INF/");

... and it worked for my Tomcat :)

I hope it saves one's day

Thanks




回答2:


I think you misunderstand how servlets work. A "servlet" is just a class configured via the web.xml file to process requests on a given path at a URL. It's in now way related to the file system at all.

Why would you want to create a folder relative to your servlet's URL? What are you trying to accomplish?



来源:https://stackoverflow.com/questions/8017343/gwt-rpc-get-servlet-absolute-path-to-make-dir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!