小编典典

找不到Http 404资源

tomcat

我是servlet和jsp的新手。我在EclipseJEE版本上做了一个小应用程序。但是,即使在Eclipse上已正确安装Tomcat,执行我的操作时也会收到“ http404:找不到资源”错误。这是我正在使用的代码:

这是我的ServletDemo.java

package com.advancejava;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;

/**
  * Servlet implementation class ServletDemo
*/
@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })

  public class ServletDemo extends HttpServlet {
  private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ServletDemo() {
    super();
    // TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request,
        HttpServletResponse response)throws ServletException, IOException {
    response.setContentType("text/html");
    try(PrintWriter pw = response.getWriter()) {
        pw.println("<!DOCTYPE html>");
        pw.println("<html>");
        pw.println("<head>");
        pw.println("<body>");
        pw.println("<h1>Servlet Demo"+request.getContextPath()+"</h1>");
        pw.println("</body>");
        pw.println("</html>");
    }
}
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    try{
        pw.println("<html>");
        pw.println("<head>");
        pw.println("<title>Servlet</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<p>First DemoServlet</p>");
        pw.println("</body>");
        pw.println("</html>");
    }
    finally{
        System.out.println("This is my Servlet Page");
    }
}

}

web.xml

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

  <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"
metadata-complete="true" version="3.0">
<display-name>ServletDemo</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>ServletDemo</servlet-name>
    <servlet-class>com.advancejava.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletDemo</servlet-name>
    <url-pattern>/ServletDemo</url-pattern>
</servlet-mapping></web-app>

我的index.html

<!DOCTYPE html>
 <html> 
   <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
   </head>
   <body>
      Advance java
          <a href="com.advancejava.ServletDemo"></a>
   </body>
</html>

如果有任何错误,请原谅我并建议我如何纠正它们。


阅读 192

收藏
2020-06-16

共1个答案

小编典典

由于您已经在以下位置声明了servlet

@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })

不要在您的web.xml中声明两次。


Servlet的网址应为

http://localhost:8080/{contextPath}/ServletDemo

如果您使用eclipse,则默认情况下contextPath将是您的projectName


还有一件事,在您的web.xml中删除metadata-complete =“ true”。否则它将无法正常工作。

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

<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"
version="3.0">
<display-name>ServletDemo</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2020-06-16