【问题标题】:I am trying to load my servlet in the browser and it gives me this error我正在尝试在浏览器中加载我的 servlet,它给了我这个错误
【发布时间】:2015-08-21 09:57:33
【问题描述】:

这是我在尝试编译代码时看到的错误——我是否错误地传递了 URL?我想从这个示例代码中学习如何在浏览器中显示我的 servlet。

HTTP Status 404 - /Lesson41/

type Status report

message /Lesson41/

description The requested resource is not available.

Apache Tomcat/8.0.22

我的servlet代码如下:

 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 Lesson41
     */

    @WebServlet("/Lesson41")
    public class Lesson41 extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */ 

        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 + "</body></html>");
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */

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

    }

这是我的 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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloServers</display-name>
  <servlet> 
      <servlet-name>Lesson41</servlet-name>
      <servlet-class>HelloServelets-Lesson41</servlet-class>
      </servlet>

  <servlet-mapping>
          <servlet-name>Lesson41</servlet-name>
          <url-pattern>http://localhost:8080/Lesson41/</url-pattern>
      </servlet-mapping>
</web-app>

【问题讨论】:

  • 我不确定,但似乎这个:http://localhost:8080/Lesson41/ url 模式是错误的,不是 url 模式应该是 /Lesson41/ 之类的吗?
  • 我也试过了,还是不行。
  • 这是我当前时代的 HTTP 状态 404 - /Lesson41/ 类型状态报告消息 /Lesson41/ 描述 请求的资源不可用。 Apache Tomcat/8.0.22

标签: java html xml web-services servlets


【解决方案1】:

您必须采取以下步骤:

 1. Remove <Servlet> and <servlet-mapping> tags and anything in between them. Since you have used @WebServlet annotation. There is no need to declare those tags in web.XML.
  1. 在您的表单标签中,将 action 属性替换为: action="Lesson41"

  2. 不要尝试直接通过 URL 调用 Servlet,您将在请求中发布一些数据。首先尝试加载您的 HTML 页面,然后点击提交按钮以调用 Servlet 的 doPost() 方法。

【讨论】:

  • 有输入标签的 HTML/JSP 文件是什么?项目名称是什么?
【解决方案2】:

似乎有多个问题。正如错误所说

无法连接到目的地(本地主机)

很可能您的网络服务器没有运行或没有监听 8080 端口。

其次,您似乎在 URL 中缺少应用程序上下文名称:

 http://localhost:8080/Lesson41

不确定您的应用程序是什么,但也许 HelloServers 和 URL 应该是:

 http://localhost:8080/HelloServers/Lesson41

【讨论】:

  • 我试过 /Lesson41,它给了我同样的时代。我正在通过网络浏览器加载它。我的服务器已开启。
  • 嘿 @Juned 你有我可以学习的例子吗?还是推荐?
  • HTTP 状态 404 - /Lesson41/ 类型状态报告消息 /Lesson41/ 描述 请求的资源不可用。 Apache Tomcat/8.0.22
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
相关资源
最近更新 更多