Search code examples
jspservletsjakarta-eejavabeans

error 404: ressource (.jsp file) not available


I am trying to create a web application with eclipse JAVA EE IDE. It consists in a form where the customer enters his contact details( name, first name, telephone email). When you click "enter" the application display the details and an error message if you have forgot to fill a field. When I click on validate("valider"), I gate the following message:

Etat HTTP 404 - /tp1/ afficherClient.jsp type Rapport d''état message /tp1/ afficherClient.jsp ressource requested not available

here is my web.xml file:

<?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>

My servlets 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);
    }
}

My .jsp file which generate the form 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>

My jsp file which display the details 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>

My 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


Solution

  • You're using servlet API 3.0. So you don't need to specify in your web.xml file the servlet mapping. It is already mapped as an annotation in your Servlet. @WebServlet("/CreationClient")

    Remove the servlet mapping from your web.xml so it looks like this:

        <?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>
    

    And in your .jsp file you need to make sure that your action attribute in your form matches the url mapping for your servlet

    this is wrong:

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

    it needs to be this:

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

    Because in your servlet you have mapped it here @WebServlet("/CreationClient"):

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

    Update, in response to your propertynotfound error: change your Client class to this:

    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;
        }
    
    }
    

    Now this is important, whenever you call these variables in your jsp you need to make sure that it matches what you named them, so :

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

    Then you can call it like this in your jsp:

    ${client.nom}