【问题标题】:404 error with java servlets. can't see html filejava servlet 出现 404 错误。看不到html文件
【发布时间】:2019-12-05 13:10:28
【问题描述】:

我在 youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) 上关注 derek banas 的教程,当我尝试运行代码时,浏览器显示状态 404 和
消息:/Lesson41/
描述:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。

我收到 IllegalArguementsException 说 2 个不同的 servlet 被映射到相同的 url 模式。所以我删除了@WebServlet 注释。 现在我得到了404,我不知道是什么原因。我认为eclipse看不到sayhello.html

代码如下: Servlet:Lesson41.java

public class Lesson41 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String usersName= request.getParameter("yourname");
        String theLang = request.getParameter("Language");

        int firstNum = Integer.parseInt(request.getParameter("firstnum"));
        int secondNum = Integer.parseInt(request.getParameter("secondnum"));
        int sumONum = firstNum + secondNum;

        response.setContentType("text/html");

        PrintWriter output = response.getWriter();

        output.println("<html><body><h3>Hello " + usersName);
        output.println("</h3><br />" + firstNum + " + " + secondNum); 
        output.println(" = " + sumONum + "<br />Speaks " + theLang); 
        output.println("</body></html>");
 }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
    <servlet>
        <servlet-name>Lesson41</servlet-name>
        <servlet-class>helloservlets.Lesson41</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Lesson41</servlet-name>
        <url-pattern>/Lesson41</url-pattern>
    </servlet-mapping>
</web-app>

html:sayhello.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>

<form method="post" action="http://localhost:8080/Lesson41/">
    What's your name?<br />
    <input name="yourname" /><br />

    First Number<br />
    <input name="firstnum" /><br />

    Second Number<br />
    <input name="secondnum" /><br />

    <input type="hidden" name="Language" value="English" /><br />

    <input type="submit" />
</form>
</body>
</html>

目录结构: directory Structure

预期输出: 它应该显示一个表单,其中包含名称、编号 1、编号 2 和提交按钮的字段。它正确地转到 localhost:8080/Lesson41/ 但看不到 html。

【问题讨论】:

    标签: java xml servlets http-status-code-404 illegalargumentexception


    【解决方案1】:

    将 url 模式更新为 /Lesson41/

    <url-pattern>/Lesson41/</url-pattern>
    

    将表单操作更改为 /Lesson41/ 并尝试。可能会对你有所帮助。

    <form method="post" action="/Lesson41/">
    
    </form>
    

    另外,在映射时,您使用 servlet-class 作为“helloservlets.Lesson41”,需要添加包。 servlet“Lesson41”中的“helloservlets”。

    【讨论】:

    • 我刚刚尝试了您的建议,它给出了相同的 404 消息。我也再次检查。包已经在 servlet 中。
    • 您可以查看 - stackoverflow.com/questions/11731377/…。可能对你有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2012-05-15
    • 2016-11-14
    • 1970-01-01
    • 2021-11-25
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多