我试图在浏览器中加载我的servlet,它给了我这个错误

时间:2015-06-06 13:50:57

标签: java html xml web-services servlets

这是我在尝试编译代码时看到的错误 - 我是不是直接传递了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>

2 个答案:

答案 0 :(得分:0)

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

  

无法连接到目标(localhost)

很可能您的网络服务器未运行或未在8080端口上侦听。

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

 http://localhost:8080/Lesson41

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

 http://localhost:8080/HelloServers/Lesson41

答案 1 :(得分:0)

您必须采取以下步骤:

+files: FileBag {#244 ▼
#parameters: array:1 [▼
  "file" => UploadedFile {#27 ▼
    -test: false
    -originalName: "cv.pdf"
    -mimeType: "application/pdf"
    -size: 28141
    -error: 0
  }]}
  1. 在您的表单标记中,将action属性替换为: 行动=&#34; Lesson41&#34;

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