【问题标题】:When I add <servlet-mapping> to web.xml, welcome files do not appear when I go to web page当我将 <servlet-mapping> 添加到 web.xml 时,当我转到网页时不会出现欢迎文件
【发布时间】:2017-03-20 04:43:40
【问题描述】:

我向我的 Web 应用程序和一个 servlet 映射添加了一个 servlet,但是当我转到我的网页时,现在除了一个空白屏幕之外什么都没有,并且在开发人员控制台中没有错误。有人可以帮忙吗?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>BasicJavaWeb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>AuthenticatorServlet</display-name>
    <servlet-name>AuthenticatorServlet</servlet-name>
    <servlet-class>AuthenticatorServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AuthenticatorServlet</servlet-name>
    <url-pattern>authenticator</url-pattern>
    <url-pattern>authenticator</url-pattern>
  </servlet-mapping>
</web-app>

在我添加 servlet/servlet-mappings 之前,这个 web.xml 文件确实显示了 index.html:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>BasicJavaWeb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

那么仅添加 servlet 会出现什么问题?

【问题讨论】:

  • 检查您的控制台。你可能有一个错误。并且该错误可能是由于您的 servlet 类位于默认包中。永远永远不会将任何类放在默认包中。
  • 谢谢!我确实检查了控制台并没有看到任何错误 - 但我使用的是默认包并将其移至新包。但它仍然无法正常工作。

标签: xml servlets web web.xml


【解决方案1】:

在您拥有的 servlet 映射中

<url-pattern>authenticator</url-pattern>
<url-pattern>authenticator</url-pattern>

我不明白你为什么要声明两次,但无论如何标签值应该以“/”开头

<url-pattern>/authenticator</url-pattern>
<url-pattern>/authenticator</url-pattern>

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2010-12-26
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多