Servlet Mappings Jetty 8

时间:2016-06-13 14:16:31

标签: servlets web-applications jetty web.xml servlet-mapping

我有一个适用于Tomcat 8和Jetty 8的webapp。最近,我们引入了servlet-mappings

    <servlet>
        <servlet-name>page</servlet-name>
        <jsp-file>/Dashboard.html</jsp-file>
        <load-on-startup>1</load-on-startup>
    </servlet>

   <servlet-mapping>
        <servlet-name>page</servlet-name>
        <url-pattern>/editjob</url-pattern>
    </servlet-mapping>

当您使用&#34; http://localhost:8080/app/editjob&#34;添加网址时,将tomcat重定向到&#34; http://localhost:8080/app/Dashboard.html&#34;。但是当我们使用embed Jetty部署它时,404 Not Found它将被返回。似乎servlet映射不起作用。

HTTP ERROR 404
Problem accessing /app/page. Reason:
Not Found
Powered by Jetty://

我可以直接使用进入应用程序 http://localhost:8080/app/Dashboard.html。没有映射,我们对码头没有问题 这是嵌入Jetty的代码:

final Server server = new Server();
SocketConnector connector = new SocketConnector();
connector.setMaxIdleTime(-1);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
final WebAppContext context = new WebAppContext();
context.setServer(server);
context.setContextPath("app");
context.setWar(location.toExternalForm());
server.setHandler(context);
context.start();  
server.join();

这是带有映射的web.xml。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>app</display-name>
<servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
     <servlet>
        <servlet-name>login</servlet-name>
        <jsp-file>/Login.html</jsp-file>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>page</servlet-name>
        <jsp-file>/Dashboard.html</jsp-file>
        <load-on-startup>1</load-on-startup>
    </servlet>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>
        org.springframework.web.context.ContextLoaderServlet
    </servlet-class>
</servlet> 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE, OPTIONS</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Origin, Content-Type, X-Requested-With, Access-Control-Allow-Origin</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>-1</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>page</servlet-name>
        <url-pattern>/home</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>page</servlet-name>
        <url-pattern>/jobs</url-pattern>
    </servlet-mapping>

   <welcome-file-list>
    <welcome-file>Index.html</welcome-file>

</welcome-file-list>
</web-app>

我试图在启动服务器时定义servlet-mappings,但是没有用。 这个映射与Tomcat完美配合,但Jetty ......我不知道哪个可能是问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

Problem accessing /app/page.

该网址未映射到您粘贴的web.xml

中的任何位置

具有网址格式(在<servlet-mapping>条目中定义)的servlet是:

  • /webresources/*
  • /login
  • /home
  • /jobs

这些都不会匹配/page的请求URL,导致DefaultServlet(默认映射到/)返回404请求的资源。

此外,您还没有在embedded-jetty环境中设置JSP,因此没有使用JSP。

从之前的回答https://stackoverflow.com/a/28483349/775715

复制

如果该代码段代表了如何初始化您的webapp,那么您将缺少很多的JSP初始化步骤。

由Jetty Project制作的一个示例项目展示了如何在嵌入式Jetty中使用JSP

https://github.com/jetty-project/embedded-jetty-jsp

注意......

  • 您需要的依赖项
  • 所需的ClassLoader类型
  • scratchDir声明
  • ServletContainerInitializer设置
  • Jsp Servlet Holder和映射
  • InstanceManager

(仅举几个大的)