IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Servlets/JSP Java Discussion :

Etat HTTP 500 - "Servlet.init()" pour la servlet


Sujet :

Servlets/JSP Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 174
    Par défaut Etat HTTP 500 - "Servlet.init()" pour la servlet
    Salut ,

    Voila j'utilise Tomcat 7 sur Eclipse , dans mon application Commande_Projet , j essaye de charger le DAOFactory dans la methode init() de ma servlet ClientInscription mais quand j'exécute ca me donne cette erreur sur le navigateur (l'image de l'erreur dans la piece jointe) :

    javax.servlet.ServletException: "Servlet.init()" pour la servlet ClientInscription a généré une exception

    Sur la console ca me donne cette erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Exception lors de l'allocation pour la servlet ClientInscription
    java.lang.NullPointerException
    	at com.commande.servlets.ClientInscription.init(ClientInscription.java:52)
    	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    	at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Bref l'erreur viens surement de la methode init() de ma servlet mais je ne sais pas comment résoudre le problème , voila mes code :

    => code de la servlet ClientInscription :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    public class ClientInscription extends HttpServlet {
        private static final long   serialVersionUID     = 1L;
     
        private static final String vue_formulaireClient = "/WEB-INF/formulaire_client.jsp";
        private static final String vue_menu             = "/WEB-INF/Menu.jsp";
     
        private static final String Form_Client          = "formClient";
        private static final String Bean_Client          = "beanClient";
     
        private static final String session_client       = "sessionClient";
     
        private ClientDAO           clientDAO;
     
        public static final String  DAOfactoryInstance   = "FactoryDao";
     
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ClientInscription() {
            super();
            // TODO Auto-generated constructor stub
        }
     
        /**
         * @see Servlet#init(ServletConfig)
         */
        public void init() throws ServletException {
            this.clientDAO = ( (DAOFactory) getServletContext().getAttribute( DAOfactoryInstance ) ).getClientDAO();
        }
     
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                IOException {
            this.getServletContext().getRequestDispatcher( vue_formulaireClient ).forward( request, response );
        }
     
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                IOException {
            ClientForm form_client = new ClientForm( clientDAO );
            Client client = form_client.FormClient( request );
     
            request.setAttribute( Form_Client, form_client );
            request.setAttribute( Bean_Client, client );
     
            HttpSession session = request.getSession();
     
            if ( form_client.getErreurs().isEmpty() ) {
     
                // Map Commande :
     
                Map<Long, Client> mapClient = (HashMap<Long, Client>) session.getAttribute( session_client );
     
                if ( mapClient == null ) {
                    mapClient = new HashMap<Long, Client>();
                }
     
                mapClient.put( client.getId_client(), client );
     
                session.setAttribute( session_client, mapClient );
     
                this.getServletContext().getRequestDispatcher( vue_menu ).forward( request, response );
     
            } else {
                this.getServletContext().getRequestDispatcher( vue_formulaireClient ).forward( request, response );
            }
        }
     
    }
    => code de l'initianisation du DAOFactory InisialisationDAOfactory :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class InisialisationDAOfactory implements ServletContextListener {
     
        private DAOFactory         daoFactory;
     
        public static final String DAOfactoryInstance = "FactoryDao";
     
        @Override
        public void contextInitialized( ServletContextEvent event ) {
            ServletContext context = event.getServletContext();
            this.daoFactory = DAOFactory.getInstance();
            context.setAttribute( DAOfactoryInstance, this.daoFactory );
        }
     
        @Override
        public void contextDestroyed( ServletContextEvent event ) {
            // TODO Auto-generated method stub
     
        }
     
    }
    => code du DAOFactory :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    public class DAOFactory {
     
        private static String fichierDAO           = "/com/commande/dao/dao.properties";
     
        private static String champ_url            = "url";
        private static String champ_driver         = "driver";
        private static String champ_nomutilisateur = "nomutilisateur";
        private static String champ_motdepasse     = "motdepasse";
     
        private String        url;
        private String        nomUtilisateur;
        private String        motDePasse;
     
        public DAOFactory( String url, String nomUtilisateur, String motDePasse ) {
            super();
            this.url = url;
            this.nomUtilisateur = nomUtilisateur;
            this.motDePasse = motDePasse;
        }
     
        public static DAOFactory getInstance() throws DAOConfigurationException {
     
            Properties properties = new Properties();
     
            String url;
            String driver;
            String nomUtilisateur;
            String motPasse;
     
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream fichier = classLoader.getResourceAsStream( fichierDAO );
     
            if ( fichier == null ) {
                throw new DAOConfigurationException( "Echec de lecture du fichier DAO" );
            }
     
            try {
     
                properties.load( fichier );
     
                url = properties.getProperty( champ_url );
                driver = properties.getProperty( champ_driver );
                nomUtilisateur = properties.getProperty( champ_nomutilisateur );
                motPasse = properties.getProperty( champ_motdepasse );
     
            } catch ( IOException e ) {
                throw new DAOConfigurationException( "Echec de chargement du fichier DAO" + e );
            }
     
            try {
     
                Class.forName( driver );
     
            } catch ( ClassNotFoundException e ) {
                throw new DAOConfigurationException( "Echec de chargement du Driver" + e );
            }
     
            DAOFactory instance = new DAOFactory( url, nomUtilisateur, motPasse );
            return instance;
     
        }
     
        Connection getConnection() throws SQLException {
            return DriverManager.getConnection( url, nomUtilisateur, motDePasse );
        }
     
        public ClientDAO getClientDAO() {
            return new ClientDAOimplement( this );
     
        }
    }
    => Code du ClientDAO :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public interface ClientDAO {
     
        void creerClient( Client client ) throws DAOException;
     
    }
    => Code de l'implementation ClientDAOimplement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    public class ClientDAOimplement implements ClientDAO {
     
        public static final String sqlInsert = "insert into client(nom,prenom,adresseLivraison,telephone,email) values (?,?,?,?,?)";
     
        public DAOFactory          daoFactory;
     
        public ClientDAOimplement( DAOFactory daoFactory ) {
            super();
            this.daoFactory = daoFactory;
        }
     
        @Override
        public void creerClient( Client client ) throws DAOException {
     
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultsetKey = null;
     
            try {
     
                connection = daoFactory.getConnection();
     
                preparedStatement = inisialisationPrepareStatement( connection, sqlInsert, true, client.getNom(),
                        client.getPrenom(), client.getAdresse_livraison(), client.getTelephone(), client.getEmail() );
     
                int status = preparedStatement.executeUpdate();
     
                if ( status == 0 ) {
                    throw new DAOException( "Aucune ligne n'as étais inserer" );
                }
     
                resultsetKey = preparedStatement.getGeneratedKeys();
     
                if ( resultsetKey.next() ) {
     
                    client.setId_client( resultsetKey.getLong( 1 ) );
     
                } else {
                    throw new DAOException( "L'id du client n'as pas étais générer " );
                }
     
            } catch ( SQLException e ) {
                throw new DAOException( "Echec de la création du Client" + e );
            } finally {
                fermeture( resultsetKey, preparedStatement, connection );
            }
     
        }
    Et voila l'image de mon erreur sur le navigateur :
    Images attachées Images attachées  

  2. #2
    Membre expérimenté
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2011
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 196
    Par défaut
    A mon avis c'est l'instruction suivante
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ( (DAOFactory) getServletContext().getAttribute( DAOfactoryInstance ) )
    dans ta méthode init qui te renvoie null.

    Où est-ce que tu as défini cet attribut ? Dans le web.xml ?

  3. #3
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 174
    Par défaut
    Ah ouii fallait déclarer dans le web.xml !! pourtant j'étais sur de l'avoir intégrer ! merci !

Discussions similaires

  1. Etat HTTP 500 - Erreur à l'instantiation de la classe servlet
    Par lionceau dans le forum Tomcat et TomEE
    Réponses: 4
    Dernier message: 13/03/2014, 14h32
  2. Tomcat Erreur - ETAT HTTP 500
    Par othmanbenhalima dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 16/11/2006, 12h26
  3. Etat HTTP 500
    Par gloglo dans le forum Struts 1
    Réponses: 8
    Dernier message: 22/09/2006, 11h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo