java - index.html 不呈现 servlet 3.0 Tomcat 7

标签 java html tomcat servlet-3.0

我有一个简单的 Web 应用程序,它使用 Servlet 3.0 使用 Tomcat 7.0.25 并将该应用程序部署为 war (mywebapp.war)。

war 结构如下:

  • mywebapp/index.html
  • mywebapp/META-INF/MANIFEST.MF
  • mywebapp/WEB-INF/classes/org/iq/adapter/ServerAdapter.class

index.html:

<html>
  <head>
    <title>my web app</title>
  </head>
  <body>
    <h1>Your server is Up and Running</h1>
  </body>
</html>

ServerAdapter.java:

package org.iq.adapter;

@WebServlet(name="ServerAdapter", urlPatterns="/adapter/*")
public class ServerAdapter extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        System.out.println(this.getClass()+"::doGet called");
    }

    @Override
    public void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        System.out.println(this.getClass()+"::doPost called");
    }
}

当我尝试使用以下链接从浏览器访问 mywebapp 时:

  • localhost:8080/mywebapp - 我得到一个空白屏幕,未呈现 index.html - 为什么? 我想因为没有提到欢迎文件,因为我不是使用 web.xml
  • localhost:8080/mywebapp/index.html - 我得到一个空白屏幕,index.html 仍然没有呈现 - 为什么?我迷路了
  • localhost:8080/mywebapp/adapter - 我得到一个空白屏幕,但我在服务器控制台上得到的系统输出为“class org.iq.adapter.ServerAdapter::doGet called” - < strong>符合预期

最佳答案

像下面这样在Web-INF文件夹中创建一个web.xml文件,并在欢迎文件列表中添加index.html

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

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

  <servlet>
    <servlet-name>ServerAdapter </servlet-name>
    <servlet-class>org.iq.adapter.ServerAdapter </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServerAdapter </servlet-name>
    <url-pattern>/adapter</url-pattern>
</servlet-mapping>
</web-app>  

关于java - index.html 不呈现 servlet 3.0 Tomcat 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872495/

相关文章:

java - 从 Java 执行带反引号的 Unix 命令

java - 为什么 log4j 消息会产生 UTC 时间戳?

web-services - 在哪个 XML 中插入 RemoteAddrValve 过滤器?

java - 为什么在类路径中(在 WEB-INF/lib 下)我无法使用 getResourceAsStream 读取属性文件

javascript - 使用javascript仅更改元素中的文本

java - PreparedStatement 和 Axis2

java - 如何在不切割 Java 之间的单词的情况下获取子字符串?

java - 如何在 Android 的 xml 上创建我的自定义属性?

javascript - 在 HTML5 Canvas 上同时追踪两条​​路径

jquery - 如何正确显示 AJAX post 数据返回