我有一个简单的 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.xmllocalhost: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/