gpt4 book ai didi

java - 找不到 HTTP 404 资源

转载 作者:行者123 更新时间:2023-11-27 23:30:16 25 4
gpt4 key购买 nike

<分区>

我是 servlet 和 jsp 的新手。我在 Eclipse JEE 版本上做了一个小应用程序。但是当我执行时,即使 Tomcat 已正确安装在 Eclipse 上,我也会收到“http 404: resource not found”错误。这是我正在使用的代码:

这是我的 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");
}
}

网络.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>

如有错误,请多多包涵并建议改正。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com