【问题标题】:error 404 Communication servlet/JSP in a form表单中的错误 404 通信 servlet/JSP
【发布时间】:2018-10-30 12:15:47
【问题描述】:

我正在尝试使用名称和密码创建一个简单的表单。但是,当我单击表单上名为“inscription”的验证按钮时,出现错误 404。我的错误是:ETAT 404 HTTP-/inscription 类型:Rapport d'état 消息:/题字 说明:资源不可用 这就是我的全部。

我应该得到一个空白页面,因为我的 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>

【问题讨论】:

    标签: jsp servlets jakarta-ee


    【解决方案1】:

    错误 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">
    

    编辑:

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

    在您的 web.xml 中是:

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

    但是你有一个 servlet 注释:

    @WebServlet( "/Inscription" )
    

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

    @WebServlet( "/inscription" )
    

    URL Servlet 映射区分大小写。

    【讨论】:

    • 它不适用于
      "> 。问题一定出在其他地方。
    • 请编辑您的问题并包含您遇到的整个错误
    • @Slim 查看我编辑的答案,发现您的其他问题。
    • 它仍然不起作用,即使在你告诉我的修改之后。
    • 重启服务器。确保注解和 web.xml 的 url 映射相同。发布您收到的错误代码,这样如果问题仍然存在,我们就可以实际查看问题所在。
    猜你喜欢
    • 2014-06-04
    • 2014-02-01
    • 2011-03-21
    • 2018-02-01
    • 1970-01-01
    • 2012-05-23
    • 2015-11-15
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多