我将localhost:8080 / projectName / Myservlet键入浏览器的地址栏,并出现404错误。
Myservlet.java
package com.test.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Myservlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException{
PrintWriter out=response.getWriter();
out.println("<html><body><h1>Today</h1>Myservlet</body></html>");
}
}
的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">
<Servlet>
<Servlet-name>servlet</Servlet-name>
<Servlet-class>Myservlet</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name>servlet</Servlet-name>
<url-pattern>/Myservlet</url-pattern>
</Servlet-mapping>
</web-app>
答案 0 :(得分:0)
在web.xml中,您没有定义该类的完整路径:
<Servlet>
<Servlet-name>servlet</Servlet-name>
<Servlet-class>com.test.servlet.Myservlet</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name>servlet</Servlet-name>
<url-pattern>/Myservlet</url-pattern>
</Servlet-mapping>
com.test.servlet
是Myservlet类的包名。请使用课程的完整路径。