简体   繁体   English

GWT-如何读取服务器上的(文本)文件(或:如何在RemoteServiceServlet中获取ServletContext)

[英]GWT - How to read a (text)file on the server (or: how to get the servletContext in RemoteServiceServlet)

For my GWT/GAE app I have a very large text file that I want to be read on the servlet in a normal way (eg Reader in = new InputStreamReader (VideoBroadcastServiceImpl.class.getResourceAsStream("com/myapp/resources/textfile") ) like you would do it on your local machine. The problem is that I get a NullPointerException in this line. I have read hints of a more complicated way to read a file that exists in the WEB-INF folder in a GWT project where I need the servletContext. Once I have the servletContext I can ask for the real path where the file exists. The problem of this way is that I need to implement the getServletContext() method by myself which is very easy in a HTTPServlet but not in a RemoteServiceServlet (the standard servlet of GWT for RPC calls which I also use). Is there another easy solution to read a file on the server or can you give me an advice how I can get the servletContext in a RemoteServiceServlet? 对于我的GWT / GAE应用程序,我有一个非常大的文本文件,希望以常规方式在servlet上读取它(例如Reader in = new InputStreamReader (VideoBroadcastServiceImpl.class.getResourceAsStream("com/myapp/resources/textfile") ),就像您将在本地计算机上执行此操作一样。 问题是我在此行中收到了NullPointerException。我已阅读了一些更复杂的方法,可以读取GWT项目中WEB-INF文件夹中存在的文件需要ServletContext。一旦有了ServletContext,我就可以要求文件所在的真实路径,这种方式的问题是我需要自己实现getServletContext()方法,这在HTTPServlet中非常容易,而在HTTPServlet中则不容易。 RemoteServiceServlet(我也使用的用于RPC调用的GWT的标准servlet)。是否有另一种简单的解决方案来读取服务器上的文件,或者您可以给我一些建议,如何在RemoteServiceServlet中获取servletContext?

Thank you very much in advance! 提前非常感谢您!

A RemoteServiceServlet is an HttpServlet , so it has a getServletContext() method. RemoteServiceServlet HttpServlet ,因此它具有getServletContext()方法。

(you can also get the HttpServletRequest and HttpServletResponse with getThreadLocalRequest() and getThreadLocalResponse() respectively) (您还可以分别通过getThreadLocalRequest()getThreadLocalResponse()获得HttpServletRequestHttpServletResponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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