从jsp-page运行servlet

时间:2012-09-25 14:29:36

标签: java tomcat servlets

我有index.jsp页面:

</head>
<body>
     <form action="HelloWorldServlet" method="post">
        <input class="button" type="submit" value="load list" />
     </form>
</body>
</head>

我想通过点击“加载列表”按钮来运行我的HelloWorldServlet。

public class HelloWorldServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        writer.println("Hello, World!");
        writer.close();
    }
}

但不是“你好,世界!”而不是消息,它尝试加载http://localhost:8080/HelloWorld/HelloWorldServlet页面..重点是什么?

这是我的web.xml:

<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD 
  Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>HelloWorldServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:1)

如果您的servlet映射到/hello,那么您的<form>应该发布到同一个网址。

<form action="hello" method="post">