将 servlet 移植到 Web 服务 - 访问上下文?
考虑一个简单的 servlet:
// MyServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
UtilClass.doSomething(getServletContext().getRealPath(SOME_FILE));
}
实用程序类对文件执行某些操作:
// UtilClass.java
public String doSomething(String filePath)
{
File f = new File(filePath);
String s = readWhateverFrom(f);
return s;
}
我现在将 doSomething()
函数移植到在 Tomcat 和 Axis2 下运行的 Web 服务。我该如何移植它,以便我仍然可以访问上下文并访问 servlet 下的文件?
Consider a simply servlet:
// MyServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
UtilClass.doSomething(getServletContext().getRealPath(SOME_FILE));
}
And the utility class does something with the file:
// UtilClass.java
public String doSomething(String filePath)
{
File f = new File(filePath);
String s = readWhateverFrom(f);
return s;
}
I am now porting the doSomething()
function to a web service running under Tomcat and Axis2. How would I port it so that I can still access the context and get access to a file under the servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该获取您的 (jax-ws)
MessageContext
。这取决于您的配置,但也许在您的方法中使用 和 :
编辑:似乎 Axis2 (以及 Axis)支持以下内容:
通过以下导入:
You should get ahold of your (jax-ws)
MessageContext
. This would depend on your configuration, but perhaps usingand in your method:
Edit: Seems like Axis2 (as well as Axis) support the following:
With the following imports:
听起来像是 Servlet
Filter
和ThreadLocal
的工作。 Axis 也在 Servlet 上下文中运行。因此,您所要做的就是实现一个自定义javax.servlet.Filter
,将ServletRequest
填充到ThreadLocal
中,您可以在其中访问它从您的实用程序类中。您可以从FilterConfig
获取ServletContext
。Sounds like a job for a Servlet
Filter
and aThreadLocal
. Axis is running within a Servlet Context, too. So all you have to do is to implement a customjavax.servlet.Filter
, stuffing in theServletRequest
into aThreadLocal
where you can access it from within your utility class. You can get theServletContext
from theFilterConfig
.