Java Servlet支持POST,但表单无论如何都会返回错误

时间:2016-03-15 17:57:19

标签: java jsp servlets

我有一个在servlet中调用post方法的表单。我一直得到“HTTP状态405 - 此URL不支持HTTP方法POST”错误,但我已经盯着它看了很长时间,我看不出有什么问题。我已经覆盖了doPost方法。

这是我的JSP

的表单

<%-- View all Users Section --%>
<div class="generalDisplay" id="AllUsers">
    <table class="generalTable">
        <tr>
            <th>Username</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>User Power</th>
            <th></th>
            <th></th>
        </tr>
    <c:forEach var="user" items="${AllUsers}"  varStatus="loopStatus">
        <form method="post" action="UserEditServlet">
            <tr class="${loopStatus.index % 2 == 0 ? '' : 'alt'}" id="${user.userName}">
                <td>${user.userName}<input type="hidden" value="${user.userName}" name="userName" /></td>
                <td><input class="hidableButton" disabled type="text" name="FirstName" value="${user.firstName}"</td>
                <td><input class="hidableButton" disabled type="text" name="LastName" value="${user.lastName}"</td>
                <td><input class="hidableButton" disabled type="text" name="UserPower" value="${user.userPower}"</td>
                <td class="buttonColumn"><button type="button" id="EditChangesButton" class="openInputFields">Edit User</button></td>
                <td class="buttonColumn"><button class="hidableButton" disabled id="SaveChangesButton" type="submit">Save Changes</button></td>
                </tr>
            </form>
        </c:forEach>
    </table>
</div>

这是我的Servlet

package cellar.servlets;

import cellar.models.User;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserEditServlet extends HttpServlet {

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

    //  Checks for user first
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("User");
    if((user != null) && (user.getUserPower() == 1 ))  {
        // user loged in, so continue
        User UserChanged = new User();
            UserChanged.setUserFromDB(request.getParameter("userName").toUpperCase());
        UserChanged.changeThisUser(request.getParameter("FirstName"), request.getParameter("LastName"), Integer.parseInt(request.getParameter("UserPower")));
        String url = "/RefreshAdminMenu";
            getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);
        }
    }

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String url = "/index.jsp";
            getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);

    }
}

以下是我的web.xml中的相关行

<session-config>
<servlet>
    <servlet-name>UserEditServlet</servlet-name>
    <servlet-class>cellar.servlets.UserEditServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>UserEditServlet</servlet-name>
    <url-pattern>/UserEditServlet</url-pattern>
</servlet-mapping>
</session-config>

您可以提供帮助的任何输入都会很棒。我今天正在努力解决这个问题。这个应用程序现在相当大,这是唯一不起作用的功能。我有一个类似的屏幕来编辑另一个数据库对象,它工作正常。非常感谢你的帮助。

0 个答案:

没有答案