如何从JAX-WS Web服务中访问ServletContext?

时间:2023-01-31 20:14:19

I want to share an object between my servlets and my webservice (JAX-WS) by storing it as a servlet context attribute. But how can I retrieve the servlet context from a web service?

我想通过将它存储为servlet上下文属性来共享我的servlet和我的webservice(JAX-WS)之间的对象。但是如何从Web服务中检索servlet上下文?

3 个解决方案

#1


40  

The servlet context is made available by JAX-WS via the message context, which can be retrieved using the web service context. Inserting the following member will cause JAX-WS to inject a reference to the web service context into your web service:

JAX-WS通过消息上下文提供servlet上下文,消息上下文可以使用Web服务上下文进行检索。插入以下成员将导致JAX-WS将Web服务上下文的引用注入Web服务:

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

...

@Resource
private WebServiceContext context;

Then, you can access the servlet context using:

然后,您可以使用以下命令访问servlet上下文:

ServletContext servletContext =
    (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

#2


1  

If you use Maven add this dependency!!!

如果你使用Maven添加这个依赖!

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

So I solved for avoid conflict error for get ServletContext INFO :

所以我解决了为避免冲突错误获取ServletContext INFO:

And in class method I use

在课堂上我用的方法

@WebService(endpointInterface = "choice.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Resource
    private WebServiceContext context;
    public String sayHi(String text) {
        HttpServletRequest request =(HttpServletRequest) context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
        System.out.println(request.getContextPath());

#3


-1  

If the object you are trying to share is a file, say myFile.txt you can use the method below:

如果您要共享的对象是文件,例如myFile.txt,您可以使用以下方法:

The best way to do this that I use is:

我使用的最佳方法是:

Thread.currentThread().getContextClassLoader().getResource("myFile.txt").getPath()

This gives the path of any file myFile.txt placed in /WEB-INF/classes/ directory inside the WebContent folder of the WebApp.

这会将任何文件myFile.txt的路径放在WebApp的WebContent文件夹中的/ WEB-INF / classes /目录中。

In Eclipse JEE environment you need to keep the file myFile.txt, that you may want to read within the Web Service, in the src folder for it to be transported to the /WEB-INF/classes/ folder by the deployer.

在Eclipse JEE环境中,您需要保留文件myFile.txt(您可能希望在Web服务中读取),该文件位于src文件夹中,以便由部署者将其传输到/ WEB-INF / classes /文件夹。

#1


40  

The servlet context is made available by JAX-WS via the message context, which can be retrieved using the web service context. Inserting the following member will cause JAX-WS to inject a reference to the web service context into your web service:

JAX-WS通过消息上下文提供servlet上下文,消息上下文可以使用Web服务上下文进行检索。插入以下成员将导致JAX-WS将Web服务上下文的引用注入Web服务:

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

...

@Resource
private WebServiceContext context;

Then, you can access the servlet context using:

然后,您可以使用以下命令访问servlet上下文:

ServletContext servletContext =
    (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

#2


1  

If you use Maven add this dependency!!!

如果你使用Maven添加这个依赖!

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

So I solved for avoid conflict error for get ServletContext INFO :

所以我解决了为避免冲突错误获取ServletContext INFO:

And in class method I use

在课堂上我用的方法

@WebService(endpointInterface = "choice.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Resource
    private WebServiceContext context;
    public String sayHi(String text) {
        HttpServletRequest request =(HttpServletRequest) context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
        System.out.println(request.getContextPath());

#3


-1  

If the object you are trying to share is a file, say myFile.txt you can use the method below:

如果您要共享的对象是文件,例如myFile.txt,您可以使用以下方法:

The best way to do this that I use is:

我使用的最佳方法是:

Thread.currentThread().getContextClassLoader().getResource("myFile.txt").getPath()

This gives the path of any file myFile.txt placed in /WEB-INF/classes/ directory inside the WebContent folder of the WebApp.

这会将任何文件myFile.txt的路径放在WebApp的WebContent文件夹中的/ WEB-INF / classes /目录中。

In Eclipse JEE environment you need to keep the file myFile.txt, that you may want to read within the Web Service, in the src folder for it to be transported to the /WEB-INF/classes/ folder by the deployer.

在Eclipse JEE环境中,您需要保留文件myFile.txt(您可能希望在Web服务中读取),该文件位于src文件夹中,以便由部署者将其传输到/ WEB-INF / classes /文件夹。