gpt4 book ai didi

tomcat - ReSTLet Servlet 上下文始终为空

转载 作者:行者123 更新时间:2023-11-28 22:12:08 26 4
gpt4 key购买 nike

我真的很沮丧,因为三天以来我在这个问题上没有取得任何进展。

我在 Tomcat v7.0 服务器中嵌入了一个 ReSTLet Servlet。除了访问 ServletContext 之外,一切都按预期工作。

我的目标是读取位于 WEB-INF 中的文件,就在可以找到 web.xml 的位置。

Deployment Resources
WebContent
log
META-INF
WEB-INF
lib
textFileToRead.txt <-----
web.xml

为了达到这个目的,我需要访问 ServletContext。我尝试了我可以通过网络找到的所有代码片段,包括那些我不希望工作的人,因为我正在使用 ReSTLet 2.0 jee 并且一些示例是为 ReSTLet 1.0 jee 制作的。

以下是我尝试访问上下文的方式:

Context c = this.getContext();

我调试了它并从上面制作了变量“c”的屏幕截图:

https://www.dropbox.com/s/b31a52l3w8xifgg/context_full.PNG?dl=0

如您所见,“ServerDispatcher”的“context”变量为空。但是有一个“componentContext”,其中包含我非常需要的 ServletContext。但我看不到访问它的可能性。


这里有一些关于我的配置的信息

  1. web.xml

     <display-name>MyRestServer</display-name>
    <servlet>
    <servlet-name>MyServer</servlet-name>
    <servlet-class>org.restlet.ext.servlet</servlet-class>
    <init-param>
    <param-name>org.restlet.component</param-name>
    <param-value>com.example.my.server.MyMainComponent</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
    <param-name>myParam</param-name>
    <param-value>the value</param-value>
    </context-param>
    <servlet-mapping>
    <servlet-name>MyServer</servlet-name>
    <url-pattern>/rs/*</url-pattern>
    </servlet-mapping>
  2. pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MyRestServer</groupId>
    <artifactId>MyRestServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>YaynoRestServer</name>
    <description>Maven POM von MyRestServer</description>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
    <warSourceDirectory>WebContent</warSourceDirectory>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
    </plugin>
    </plugins>
    </build>

    <repositories>
    <repository>
    <id>maven-restlet</id>
    <name>Public online Restlet repository</name>
    <url>http://maven.restlet.com</url>
    </repository>
    </repositories>

    <dependencies>
    <dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet</artifactId>
    <version>2.3.1</version>
    </dependency>

    <dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet.ext.servlet</artifactId>
    <version>2.3.1</version>
    </dependency>

    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

    <dependency>
    <groupId>org.restlet.jse</groupId>
    <artifactId>org.restlet.ext.json</artifactId>
    <version>2.3.1</version>
    </dependency>

    <dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet.ext.jackson</artifactId>
    <version>2.3.1</version>
    </dependency>

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
    </dependency>

    <dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>3.0</version>
    </dependency>


    </dependencies>
    </project>
  3. 来源结构

基本上 MyMainComponent 使用 MyMainApplication 创建了一个默认主机

    public class MyMainComponent extends Component {
public YaynoMainComponent() {
// Starting Application
getDefaultHost().attachDefault(new MyMainApplication(questionManager.getQuestionPool(), userManager.getUserpool()));
}
}

public class MyMainApplication extends Application {
@Override
public Restlet createInboundRoot() {

// attach routes
Router router = new Router(getContext());
//router attachments...

ComponentServerDispatcher c = (ComponentServerDispatcher) this.getContext().getServerDispatcher();

return router; //breakpoint here to read out c-variable in debugger
}

最佳答案

我认为你应该使用ReSTLet 的客户端协议(protocol)war。它会自动注册到嵌入在 servlet 容器中的 ReSTLet 应用程序的上下文中。要使用它,只需尝试类似的操作:

ClientResource cr = new ClientResource(
"war:///WEB-INF/textFileToRead.txt");
Representation fileContent = cr.get();
FileRepresentation fileRepresentation
= new FileRepresentation(fileContent);

// Get stream on the file
FileInputStream fis = fileRepresentation.getStream();
// Get file content as text
String fileContentAsText = fileRepresentation.getText();

希望对你有帮助,蒂埃里

关于tomcat - ReSTLet Servlet 上下文始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com