jsp - 表单中的错误 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>
解决方案
错误 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 映射区分大小写。
推荐阅读
- enums - 如何在使用带有整数列表的 Enum.chunk_every 时删除转义字符
- mysql - MySQL 8:使用 JSON_TABLE 的子查询不起作用,除非手动输入子查询结果
- typescript - Dart Flutter 联合字符串类型
- android - 在编写请求 json 对象的函数时,在 Kotlin 中出现错误“Expecting body class”
- flutter - 当 FirebaseAuth 实例启动时,它会在 Flutter 表单上给出错误
- c++ - 如何从函数模板的实例化推断模板参数?
- intersection - 使用 GeoPandas Python 用 shapefile 屏蔽 netcdf 文件
- javascript - 在 javascript 中选择一个选项并搜索该段落
- c++ - 如何在 RapidsAI Docker 容器中编译 C++
- php - php openssl - RSA_padding_check_PKCS1_type_2 和 RSA_EAY_PRIVATE_DECRYPT:填充检查失败:rsa_eay.c:602: