HTTP状态404 --- Spring MVC

时间:2016-11-24 18:19:13

标签: spring spring-mvc

我在tomcat 8.0.39上运行代码并首先显示login.jsp当我填写名称和密码并点击提交时,它返回404错误:login.jsp图像 HTTP Status 404 image

我的代码如下:

的web.xml

<!-- webapp/WEB-INF/web.xml -->
<web-app 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"
version="3.0">

<display-name>To do List</display-name>

<welcome-file-list>
    <welcome-file>login.do</welcome-file>
</welcome-file-list>

</web-app>

LoginServlet.java

package com.ezmsip;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/login.do")
public class LoginServlet extends HttpServlet {

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

        request.getRequestDispatcher("WEB-INF/views/login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    request.setAttribute("name", request.getParameter("name"));

    request.getRequestDispatcher("WEB-INF/views/welcome.jsp").forward(request, response);
}

}

的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hola desde una JSP</title>
</head>
<body>

<form action="/login.do" method="post">
Nombre: <input type="text" name="name" /> Password: <input type="password"      name="password" /> <input type="submit" value="Log-in" />
</form>
</body>
</html>

的welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bienvenido!</title>
</head>
<body>
Bienvenido!  ${name}
</body>
</html>

谢谢!

1 个答案:

答案 0 :(得分:0)

它并没有抱怨login.jsp。它抱怨/login.do。您是否将此应用程序部署为tomecat中的根Web应用程序?否则,它位于上下文路径下,您需要采取/myWebApp/login.do之类的操作。

无论如何,正确的做法是预先设置上下文路径:

action="${pageContext.request.contextPath}/login.do"

或使用JSTL:

action="<c:url value='/login.do'/>"