在Spring中提交后,Servlet无法访问POST方法

时间:2017-10-19 17:22:07

标签: java spring jsp spring-mvc servlets

我有一个问题,我无法找到答案。

通常情况下,我会使用IDE设置的所有内容作为默认设置,但是根据要求,使用Spring MVC,我必须删除applicationContext.xmldispatcher-servlet.xml才符合标准。当我这样做时,它停止工作,因为web.xml文件,但我设法得到Servlet的GET方法,但是,当我使用POST方法时,它抛出了我的#34;请求的资源不可用。"错误。

如何在不显示此错误的情况下使用POST方法?

这是我的index.jsp文件

<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Ejercicio usando FORM sin AJAX</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>

<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="./">Ejercicio</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="col-md-12">
            <h1>Ejercicio 1:- Usando Form y Action sin Ajax</h1>
        </div>
        <form action="../app/modulo/RecibirDatos.do" method="POST" id="form">
            <div class="form-group col-md-6">
                <label for="nombre">Nombre: </label>
                <input type="text" name="nombre" id="nombre" class="form-control" placeholder="Inserte el nombre">
            </div>
            <div class="form-group col-md-6">
                <label for="apellido">Apellido: </label>
                <input type="text" name="apellido" id="apellido" class="form-control" placeholder="Inserte el apellido">
            </div>
            <input type="submit" value="Submit" class="btn btn-primary btn-block">
        </form>
    </div>

    <script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="../js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/bootbox.min.js"></script>
    <script type="text/javascript" src="../js/main.js"></script>
</body>
</html>

这是我的servlet文件

package data.servlet;

import data.dataAccessObject.ManipulaPersona;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RecibirDatos extends HttpServlet {

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Overriding service() usually isn't needed. - The default implementation mostly
    // does the right thing&reg;
    super.service(request, response);
}

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ManipulaPersona mp = new ManipulaPersona();
    mp.crearPersona(request.getParameter("nombre"), request.getParameter("apellido"));
    request.setAttribute("persona", mp.obtenerPersona());
    request.getRequestDispatcher("../Views/resultado.jsp").forward(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}

}

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name></param-name>
    <param-value></param-value>
</context-param>
<!-- <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet> -->
<servlet>
    <servlet-name>Inicio</servlet-name>
    <servlet-class>data.servlet.Inicio</servlet-class>
</servlet>
<servlet>
    <servlet-name>RecibirDatos</servlet-name>
    <servlet-class>data.servlet.RecibirDatos</servlet-class>
</servlet> <!--
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.lol</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
    <servlet-name>Inicio</servlet-name>
    <url-pattern>/Inicio</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RecibirDatos</servlet-name>
    <url-pattern>/app/modulo/RecibirDatos.do</url-pattern>

</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>Inicio</welcome-file>
</welcome-file-list>
</web-app>

1 个答案:

答案 0 :(得分:1)

如果GET请求使用相同的代码,则POST也应该有效。

您可能需要查看以下代码行,这可能会导致失败

request.getRequestDispatcher("../Views/resultado.jsp").forward(request, response);

如果可以访问/app/Views/resultado.jsp,您需要检查吗?