Servlet未找到错误

时间:2016-04-17 12:59:18

标签: java xml servlets

我找不到servlet错误。我使用WIldFly。我的目录结构如下所示:

root - > app,converter.html,src

app - > WEB-INF

WEB-INF - > classes,lib,web.xml

src - > servlet.java

我已经查看了一段时间,无法确定问题所在。我认为我在web.xml中正确映射了映射,并且表单操作似乎也被发送到.html文件中的正确位置。

Servlet类:

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException{
    String username = request.getParameter("username");
    String email = request.getParameter("email");
    response.getWriter().println("<html>");
    response.getWriter().println("<head>");
    response.getWriter().println("<title>Title</title>");
    response.getWriter().println("</head>");
    response.getWriter().println("<body>");
    response.getWriter().println("Convert. ");
    response.getWriter().println("</body>");
    response.getWriter().println("</html>");
}


}

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>servlet</servlet-class>
  </servlet>  

  <servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
  </servlet-mapping>

</web-app>

converter.html

<!DOCTYPE html>
<html>
<head>
        <title> Test form </title>
</head>
<body>
    <form action="http://localhost:8080/root/src/servlet" method="get">
        Name: <input type="text" name="username"><br>
        Email: <input type="text" name="email"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:-3)

您需要通过以下方式修复converter.html:

  1. 将操作更改为action =“servlet”
  2. 由于您要将数据发送到服务器,因此将方法从GET更改为POST。用于从服务器检索数据的HTTP GET方法。
  3. 您可以在以下链接中阅读更多内容: http://www.tutorialspoint.com/servlets/servlets-form-data.htm