我刚刚开始学习servlet技术,目前正在尝试创建一个非常基本的web应用程序。只是几页,试试这个。我可以访问起始页/index.html,但我得到了其他部分的404 HTTP状态代码:
消息请求的资源[/first]不可用
说明源服务器找不到目标资源的当前表示形式,或者不愿意透露该表示形式的存在。
我在web-INF下的web.xml是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
</web-app>
我的第一个Servlet.java是这样的:
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<body>");
pw.println("Welcome to servlet");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
}
上下文路径为/。我在Webservlet注释中尝试了urlPattern。我正在Intellij Idea Community下尝试它,使用Smart Tomcat插件,安装Tomcat 10.1.24,并使用java 11。我运行了mvn干净的程序包。启动应用程序后,我可以访问
index.html带有http://localhost:8080/
但我够不着
http://localhost:8080/first
.
一段时间以来,我一直在努力寻找解决方案,但我没能做到。我也通读了这里的线程(至少试过了),但没有找到。从Dockerfile映像中,我用Tomcat 9.0.89尝试了它,但我得到了相同的/404http状态代码。
我想在这个问题上寻求你的帮助。如果我能提供任何有用的额外相关信息,请告诉我。
非常感谢您提前提供的帮助。