我是servlets和jsp的新手。我在Eclipse JEE版本上做了一个小应用程序。但是当我执行时,我得到了#404; http 404:资源未找到'即使Tomcat已在Eclipse上正确安装,也会出现错误。这是我正在使用的代码:
这是我的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>
如果有任何错误,请原谅我并建议我如何纠正错误。
答案 0 :(得分:2)
因为您已经在
声明了您的servlet@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })
不要在你的web.xml中声明两次。
你的servlet的url应该是
http://localhost:8080/{contextPath}/ServletDemo
如果您使用eclipse
,则默认情况下contextPath将是您的projectName还有一件事,删除metadata-complete =&#34; true&#34;在你的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"
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>
答案 1 :(得分:0)
之前的回复:索引页面应为“my index.html”或“index.html”。 它需要与servlet中的内容一致。
的index.html
发布两次真的是问题所在。我很高兴这已经解决了。它对我也有帮助。感谢