无法在按钮单击时调用Servlet

时间:2016-04-28 07:51:33

标签: java servlets

我正在研究一个简单的servelt示例,其中在vbutton上点击我在浏览器窗口上写了一些消息:

我做了以下事情:

web.xml
<web-app .... >
<welcome-file-list>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>


LoginServlet.java
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

LeaderConnection connection = null;
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
      out.println("<h1>" + "Hello" + "</h1>");
}

}


login.html
<body>
<p> This is login Page</p>

        <form action = "/LoginServlet" method = "GET">
        <input type = "submit" />

        </form>
</body>

当我在tomcat服务器上运行项目时,我正在获取登录页面内容...但是当我点击按钮并请求servlet时,它会给出以下消息:

 HTTP Status 404 - /LoginServlet
 Requested resource not available

为什么会这样?

1 个答案:

答案 0 :(得分:0)

在web.xml中映射servlet(在web-app标记内),如下所示:

<servlet>
    <servlet-name>MyLoginServlet</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyLoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>