我已经尽一切努力使该servlet正常工作,并且在下载了几个较旧的JRE和Apache tomcat的多个较旧版本之后,它仍然无法正常工作。现在,我正在将开普勒Java EE与JRE v1.7和Tomcat v7结合使用。以下是我的Java源代码和html-我怀疑问题出在doGet方法本身,因为我认为文件路径已正确命名,但我仍返回空白页。尽管问题可能出在表单动作上-我完全不了解日食。

<!DOCTYPE html>
   <html>
       <head> <meta charset="ISO-8859-1">
          <title>Insert title here</title>
       </head>
     <body>
        <form  action="/HelloWorld/HelloServlet" method="get">
            <input type="submit"  value="go to hello servlet"/>
        </form>
     </body>
   </html>`


HelloServaled.java

package com.tutorials.servlets;

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 HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {


    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
  /* @see Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request,                                                                                                           >        HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse  >      response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

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

    protected void doGet(HttpServletRequest request, HttpServletResponse           >             response) throws ServletException, IOException {


             PrintWriter out = response.getWriter();
             out.println("I am in go get mehtod" );

        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request,                                            >       HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse               >       response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
     */
    protected void doPut(HttpServletRequest request, HttpServletResponse                    >       response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

最佳答案

您的主要问题在于表单的action属性与servlet的映射不同而导致的不匹配。

您的注释:(@WebServlet("/HelloServlet"))告诉应用程序服务器侦听对“ / HelloServlet”的请求,例如:http://eample.com/HelloServlet

另一方面,您的表单将其操作设置为/HelloWorld/HelloServlet,这意味着发布请求转到了http://example.com/HelloWorld/HelloServlet

因此,当您的浏览器发布到那里时,应用程序会按应有的方式处理它,而不是应有的处理,因为您的servlet不在那儿监听任何内容。

09-05 07:31