如何映射"根"Servlet以便其他脚本仍可运行?

Jer*_*gan 53 java google-app-engine web.xml servlets

我正在尝试构建一个调用类似于以下内容的JSP页面的Servlet:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    req.getRequestDispatcher("/WEB-INF/main.jsp").forward(req, resp);
}
Run Code Online (Sandbox Code Playgroud)

我需要这个Servlet来响应域的根(例如:http://example.com/)所以我在web.xml中使用以下映射:

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是它匹配一切,所以当调度程序转发到"/WEB-INF/main.jsp"时,这与url-pattern匹配,以便Servlet再次运行.这会产生一个循环,直到它以java.lang.StackOverflowError消失.

如何在不阻止其他脚本运行的情况下匹配根目录?

nil*_*skp 46

使用空模式,例如

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

servlet 3.0规范澄清了这一点:

空字符串("")是一个特殊的URL模式,它完全映射到应用程序的上下文根

所以它至少应该在3.0容器上工作,我已经验证它适用于Jetty 8

  • 在WebSphere 8.5(servlet 3.0)中不起作用 (2认同)
  • 在谷歌应用程序引擎中不起作用 (2认同)

Jeb*_*Jeb 29

使用web.xml的welcome-file元素在app引擎上工作.这是我的:

<web-app>
    <servlet>
        <servlet-name>RootServlet</servlet-name>
        <servlet-class>com.foo.RootServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RootServlet</servlet-name>
        <url-pattern>/foo</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>foo</welcome-file>
    </welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)


Sam*_*wry 7

最初的问题没有提到他们正在尝试在App Engine上映射根servlet - 据我所知,Tomcat(以及其他servlet容器)很容易,但App Engine不是普通的servlet容器.

使用servlet构建Web应用程序的常用方法是扩展HttpServlet,添加带有标题,内容,错误,消息等的"页面"对象,并将输出转发到JSP模板.这是在App Engine中工作的绝对噩梦.

  • JSP文件不能在开头没有"/"的情况下"命名".
  • JSP文件不能位于子目录中
  • 无法使用"/"url-pattern将Servlet映射到应用程序的根目录

这是我的web.xml(为简洁而编辑),最终起作用.

<web-app>
  <servlet>
    <!-- this servlet needs to redirect to a NamedDispatcher
         called "template" -->
    <servlet-name>Home</servlet-name>
    <servlet-class>my.domain.HomeSv</servlet-class>
  </servlet>
  <servlet>
    <!-- jsp file must apparently be in root directory and have "/" at
         start of path -->
    <servlet-name>template</servlet-name>
    <jsp-file>/template.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <!-- map your home servlet to somewhere other than "/" -->
    <servlet-name>Home</servlet-name>
    <url-pattern>/home</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <!-- make your Home servlet the welcome file -->
    <welcome-file>home</welcome-file>
  </welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)

我没有特别科学地证实这一切 - 但它现在似乎对我有用,我对此感到非常高兴.


Vin*_*nie 4

您可以使用 JSTL 或其他方式使用以下代码在根目录中创建一个名为 index.jsp 的欢迎文件。

<c:redirect url="/main"/>
Run Code Online (Sandbox Code Playgroud)

因此,在 web.xml 文件中您将看到以下内容:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>        
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

因此任何请求 root 的人都会被重定向到 /main。现在您的 servlet 可以映射到 main。

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/main</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我知道这会起作用,但感觉就像是黑客攻击。难道真的不可能让 Servlet 自己处理根吗? (2认同)