我將servlet配置爲web.xml中的默認servlet。對servlet映射感到困惑
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
當我請求JSP像請求 http://localhost:8080/abc.jsp
,我得到正確的HTML內容的正確反應,並且servlet沒有服務於這個請求。
但是,當我要求HTML如請求 http://localhost:8080/abc.html
,我無法得到abc.html文件,並且我的servlet的service()
方法被調用。
爲什麼servlet容器會這樣操作?
如果我這樣配置我的servlet,是否意味着我必須在我的servlet中爲靜態文件提供請求?
更新
的web.xml
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Servlet test</display-name>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.test.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
MyServlet.java
package com.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
private static Logger LOG = LoggerFactory.getLogger(MyServlet.class);
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
LOG.info("Served");
}
}
你能在你的Servlet類共享代碼呢? –
請同時分享您的web.xml – jr593