【问题标题】:Unable to load servlet on button submit action in JSP无法在 JSP 中的按钮提交操作上加载 servlet
【发布时间】:2015-05-05 20:15:03
【问题描述】:

我有简单的login.jsp 页面:

<form class="form-horizontal" role="form" action="/SchoolERP/Login" method="post">
    <div class="form-group">
        <label for="username" class="control-label col-sm-1">Username:</label>
        <div class="col-sm-5">
            <input type="text" class="form-control" id="username" placeholder="Username">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="control-label col-sm-1">Password:</label>
        <div class="col-sm-5">
            <input type="password" class="form-control" id="password" placeholder="Password">
        </div>
    </div>
    <div class="col-sm-offset-1 col-sm-5">
        <button type="submit" class="btn btn-default">Submit</button>
    </div>
</form>

我的web.xml 文件包含以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SchoolERP</display-name>
  <servlet>
    <servlet-name>SchoolERP</servlet-name>
    <servlet-class>com.rms.school.controllers.ControllerHome</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SchoolERP</servlet-name>
    <url-pattern>/SchoolERP</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.rms.school.controllers.ControllerLogin</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/SchoolERP/Login</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <!-- This welcome filename is linked to above mentioned url-pattern -->
    <welcome-file>SchoolERP</welcome-file>
  </welcome-file-list>

</web-app>

其中ControllerLogin.java如下:

public class ControllerLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd = request.getRequestDispatcher("jsp/template.jsp");
        request.setAttribute("content_page", "admin/login.jsp");
        request.setAttribute("title", "Login");
        rd.forward(request, response);
    }

}

当我单击 Login.jsp 页面上的提交按钮时,它给出的错误为requested resource /SchoolERP/Login not found。我哪里做错了?

我在 login.jsp 中尝试了各种变体,例如 action="Login"。同样在web.xml 中,我尝试使用Login/SchoolERP/Login 等。但仍然存在错误。

【问题讨论】:

    标签: java forms jsp servlets


    【解决方案1】:

    终于解决了。我从web.xml 中删除了Login &lt;servlet&gt; 定义,并将注释添加到ControllerLogin servlet 为@WebServlet({"/Login"})。这奏效了。但是我仍然无法理解上面的代码哪里出错了?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-21
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2021-07-04
      • 1970-01-01
      相关资源
      最近更新 更多