错误404:ressource(.jsp文件)不可用

时间:2018-05-13 16:37:56

标签: jsp servlets java-ee javabeans

我正在尝试使用eclipse JAVA EE IDE创建一个Web应用程序。它包含客户输入其联系方式(姓名,名字,电话)的表格。当您单击“输入”时,如果您忘记填写字段,应用程序将显示详细信息和错误消息。当我点击验证(“valider”)时,我输入以下消息:

Etat HTTP 404 - / tp1 / afficherClient.jsp 类型Rapport d''état message / tp1 / afficherClient.jsp 请求的资源不可用

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>tp1</display-name>
  <servlet>
    <description>Servlet de création du client</description>    
    <servlet-name>CreationClient</servlet-name>
    <servlet-class>com.zdzee.tp.servlets.CreationClient</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CreationClient</servlet-name>
    <url-pattern>/creationClient</url-pattern>
  </servlet-mapping>  
</web-app>

我的servlet CreationClient:

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;

import com.sdzee.tp.beans.Client;

/**
 * Servlet implementation class CreationClient
 */
@WebServlet("/CreationClient")
public class CreationClient extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreationClient() {
        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());


    String nomClient =  request.getParameter("nomClient");
    String prenomClient =  request.getParameter("prenomClient");
    String telephoneClient =  request.getParameter("telephoneClient");
    String emailClient =  request.getParameter("emailClient");
    String adresseClient = request.getParameter( "adresseClient" );

    String message;

    if ( nomClient.trim().isEmpty() || adresseClient.trim().isEmpty() || telephoneClient.trim().isEmpty() ) {
        message = "Erreur - Vous n'avez pas rempli tous les champs obligatoires. <br> <a href=\"creerClient.jsp\">Cliquez ici</a> pour accéder au formulaire de création d'un client.";
    } else {
        message = "Client créé avec succès !";
    }
    Client client=new Client();
    client.setEmail(emailClient);
    client.setNom(nomClient);
    client.setPrenom(prenomClient);
    client.setTelephone(telephoneClient);
    client.setAdresse( adresseClient );

    request.setAttribute( "client", client );
    request.setAttribute( "message", message );

    this.getServletContext().getRequestDispatcher( "/ afficherClient.jsp" ).forward( request, response );

    }

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

我的.jsp文件生成表单creerClient.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta charset="utf-8" />
        <title>Création d'un client</title>
        <link type="text/css" rel="stylesheet" href="inc/style.css" />
    </head>
    <body>
        <div>
            <form method="get" action="creationClient">
                <fieldset>
                    <legend>Informations client</legend>

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

                    <label for="prenomClient">Prénom </label>
                    <input type="text" id="prenomClient" name="prenomClient" value="" size="20" maxlength="20" />
                    <br />

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

                    <label for="telephoneClient">Numéro de téléphone <span class="requis">*</span></label>
                    <input type="text" id="telephoneClient" name="telephoneClient" value="" size="20" maxlength="20" />
                    <br />

                    <label for="emailClient">Adresse email</label>
                    <input type="email" id="emailClient" name="emailClient" value="" size="20" maxlength="60" />
                    <br />
                </fieldset>
                <input type="submit" value="Valider"  />
                <input type="reset" value="Remettre à zéro" /> <br />
            </form>
        </div>
    </body>
</html>

我的jsp文件显示详细信息afficherClient.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!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>Affichage d'un client</title>
</head>
<body>
        <%-- Affichage de la chaîne "message" transmise par la servlet --%>
        <p class="info">${ message }</p>
        <%-- Puis affichage des données enregistrées dans le bean "client" transmis par la servlet --%>
        <p>Nom : ${ client.nomClient }</p>
        <p>Prénom : ${ client.prenomClient }</p>
        <p>Adresse : ${ client.adresseClient }</p>
        <p>Numéro de téléphone : ${ client.telephoneClient }</p>
        <p>Email : ${ client.emailClient }</p>

</body>
</html>

我的bean Client.java:

package com.sdzee.tp.beans;

public class Client {

private String nomClient;
private String prenomClient;
private String adresseClient;
private String telephoneClient;
private String  emailClient;

    public String getAdresse() {
        return this.adresseClient;
    }

    public String getNom() {
        return this.nomClient;
    }

    public String getPrenom() {
        return this.prenomClient;
    }

    public String getTelephone() {
        return this.telephoneClient;
    }

    public String getEmail() {
        return this.emailClient;
    }

    public void setAdresse( String adresse ) {
        this.adresseClient = adresse;
    }

    public void setNom( String nom ) {
        this.nomClient = nom;
    }

    public void setPrenom( String prenom ) {
        this.prenomClient = prenom;
    }

    public void setTelephone( String telephone ) {
        this.telephoneClient = telephone;
    }
    public void setEmail( String email ) {
        this.emailClient = email;
    }       

}

Here is my form

1 个答案:

答案 0 :(得分:0)

您正在使用servlet API 3.0。因此,您不需要在web.xml文件中指定servlet映射。它已作为注释映射到Servlet中。 @WebServlet("/CreationClient")

从web.xml中删除servlet映射,使其如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <display-name>tp1</display-name>

    </web-app>

在.jsp文件中,您需要确保表单中的action属性与servlet的url映射相匹配

这是错误的:

 <form method="get" action="creationClient">

需要这样:

 <form method="get" action="CreationClient">

因为在您的servlet中已将其映射到@WebServlet("/CreationClient")

/**
 * Servlet implementation class CreationClient
 */
@WebServlet("/CreationClient") //<----- this is your servlet URL
public class CreationClient extends HttpServlet {
    private static final long serialVersionUID = 1L;

更新,以响应您的propertynotfound错误:将您的客户端类更改为:

public class Client {

    String nom;
    String prenom;
    String adresse;
    String telephone;
    String  email;

    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }
    public String getPrenom() {
        return prenom;
    }
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
    public String getAdresse() {
        return adresse;
    }
    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

现在这很重要,每当你在jsp中调用这些变量时,你需要确保它与你命名的变量相匹配,所以:

String nom;
String prenom;
String adresse;
String telephone;
String  email;

然后你可以在你的jsp中这样调用它:

${client.nom}