将 servlet 移植到 Web 服务 - 访问上下文?

发布于 2024-08-16 12:55:41 字数 548 浏览 7 评论 0原文

考虑一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凶凌 2024-08-23 12:55:41

您应该获取您的 (jax-ws) MessageContext。这取决于您的配置,但也许

@Resource
private WebServiceContext wsCtx;

在您的方法中使用 和 :

MessageContext messageContext = wsCtx.getMessageContext()

ServletContext ctx = (ServletContext) 
           messageContext.getProperty(MessageContext.SERVLET_CONTEXT);

编辑:似乎 Axis2 (以及 Axis)支持以下内容:

HttpServlet servlet = (HttpServlet) 
    MessageContext.getCurrentContext().getProperty(HTTPConstants.MC_HTTP_SERVLET);
ServletContext ctx = servlet.getServletContext();

通过以下导入:

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;

You should get ahold of your (jax-ws) MessageContext. This would depend on your configuration, but perhaps using

@Resource
private WebServiceContext wsCtx;

and in your method:

MessageContext messageContext = wsCtx.getMessageContext()

ServletContext ctx = (ServletContext) 
           messageContext.getProperty(MessageContext.SERVLET_CONTEXT);

Edit: Seems like Axis2 (as well as Axis) support the following:

HttpServlet servlet = (HttpServlet) 
    MessageContext.getCurrentContext().getProperty(HTTPConstants.MC_HTTP_SERVLET);
ServletContext ctx = servlet.getServletContext();

With the following imports:

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
初心 2024-08-23 12:55:41

听起来像是 Servlet FilterThreadLocal 的工作。 Axis 也在 Servlet 上下文中运行。因此,您所要做的就是实现一个自定义 javax.servlet.Filter,将 ServletRequest 填充到 ThreadLocal 中,您可以在其中访问它从您的实用程序类中。您可以从FilterConfig 获取ServletContext

Sounds like a job for a Servlet Filter and a ThreadLocal. Axis is running within a Servlet Context, too. So all you have to do is to implement a custom javax.servlet.Filter, stuffing in the ServletRequest into a ThreadLocal where you can access it from within your utility class. You can get the ServletContext from the FilterConfig.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文