首页 > 解决方案 > 表单中的错误 404 通信 servlet/JSP

问题描述

我正在尝试使用名称和密码创建一个简单的表单。但是,当我单击名为“inscription”的表单上的验证按钮时,出现错误 404。我的错误是:ETAT 404 HTTP-/inscription type: Rapport d'état message:/inscription description: ressource not available 这就是我所拥有的.

我应该得到一个空白页,因为我的 post 方法中没有任何内容。

这是我的JSP 文件 inscription.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Inscription</title>
    <link type="text/css" rel="stylesheet" href="<c:url value="/inc/style.css"/>" />
</head>
<body>
<form method="post" action="/inscription">
            <fieldset>
                <legend>Inscription</legend>
                <p>Vous pouvez vous inscrire via ce formulaire.</p>

                <label for="email">Adresse email <span class="requis">*</span></label>
                <input type="text" id="email" name="email" value="" size="20" maxlength="60" />
                <br />

                <label for="motdepasse">Mot de passe <span class="requis">*</span></label>
                <input type="password" id="motdepasse" name="motdepasse" value="" size="20" maxlength="20" />
                <br />

                <label for="confirmation">Confirmation du mot de passe <span class="requis">*</span></label>
                <input type="password" id="confirmation" name="confirmation" value="" size="20" maxlength="20" />
                <br />

                <label for="nom">Nom d'utilisateur</label>
                <input type="text" id="nom" name="nom" value="" size="20" maxlength="20" />
                <br />

                <input type="submit" value="Inscription" class="sansLabel" />
                <br />
            </fieldset>
        </form>
</body>
</html>

这是我的servlet Inscription.java:

package com.sdzee.pro.servlets;

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;

/**
 * Servlet implementation class Inscription
 */
@WebServlet( "/inscription" )
public class Inscription extends HttpServlet {
    private static final long  serialVersionUID = 1L;
    public static final String VUE              = "/WEB-INF/inscription.jsp";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Inscription() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append( "Served at: " ).append( request.getContextPath() );
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    public void doPost( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet( request, response );
    }

}

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>Inscription</servlet-name>
        <servlet-class>com.sdzee.pro.servlets.Inscription</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Inscription</servlet-name>
        <url-pattern>/inscription</url-pattern>
    </servlet-mapping>
</web-app>

标签: jspservletsjakarta-ee

解决方案


错误 404 表示无法找到您尝试访问的资源。因此,您的 Servlet 的 url 映射存在问题。

<form method="post" action="<c:url value="/inscription"/>">

这是不正确的,您需要像这样转义引号:

<form method="post" action="<c:url value=\"inscription\">">

但是,如果您知道您的 URL 映射,为什么不使用相对路径对其进行硬编码呢?('..' 用于向上目录)

<form method="post" action="../inscription">

编辑:

刚刚注意到您的网址映射不一致...

在您的 web.xml 中是:

 <url-pattern>/inscription</url-pattern>

但是您有一个 servlet 注释:

@WebServlet( "/Inscription" )

这也会给你带来问题。确保这些是相同的。更改注释以匹配您的 web.xml servlet 映射:

@WebServlet( "/inscription" )

URL Servlet 映射区分大小写。


推荐阅读