0

当我尝试提交由 servlet 处理的表单时,浏览器指示请求的资源不可用,并出现 HTTP 404 错误。网址如下所示:

http://localhost:8080/Website/DateSearchServlet

我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
    <servlet-name>DateSearchServlet</servlet-name>
    <servlet-class>DB.DateSearchServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DateSearchServlet</servlet-name>
    <url-pattern>/Search/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

要调用 servlet,我使用这种简单的形式:

<form name="dateSearch" action="DateSearchServlet" method="post">
    <label for="from">Date From</label>
    <input type="text" id="from" name="from" />
    <label for="to">Date To</label>
    <input type="text" id="to" name="to" />
    <input type="submit" id="submit" value="Submit">
</form>

最后这是 servlet:

@WebServlet(name = "DateSearchServlet", urlPatterns = {"/Search"})
public class DateSearchServlet extends HttpServlet {

DatabaseConnector dataManager;
boolean dbOK = false;
HomeList homes = new HomeList();

@Override
public void init() {
    dataManager = new DatabaseConnector();
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String dateFrom = request.getParameter("from");
    String dateTo = request.getParameter("to");

    if (dateFrom == null || dateTo == null) {
        dbOK = false;
    } else {
        dbOK = true;
    }//end if

    request.setAttribute("homeList", null);

    if (dbOK) {
        homes = dataManager.getPropertiesSearch(dateFrom, dateTo);
        request.setAttribute("homeList", homes);
        request.getRequestDispatcher("bookings.jsp").forward(request, response);
    } else {
        System.out.println("DBNOTOK");
        //error...
    }
}
}

谢谢

4

2 回答 2

2

web.xml 中的 URL 模式没有为您调用的 servlet 正确配置。

因此,将表单中的操作更改为 URL Search/DateSearchServlet

于 2013-01-23T10:04:27.563 回答
0

试试这个....

<form name="dateSearch" action="DateSearchServlet.action" method="post">
<label for="from">Date From</label>
<input type="text" id="from" name="from" />
<label for="to">Date To</label>
<input type="text" id="to" name="to" />
<input type="submit" id="submit" value="Submit">

于 2013-01-23T10:17:10.447 回答