Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [Servlet] HTTP Status 404 - Servlet .. is not available!!!

    Ciao.
    Ho già letto parecchi post su questo tipo di errore ma ancora non ho trovato una soluzione che vada bene x la mia applicazione...e sono 24 ore che ci riprovo!!!

    L'errore che mi da è questo:
    --------------------------------------------------------------------------------------------
    HTTP Status 404 - Servlet Survey is not available

    type: Status report

    message: Servlet Survey is not available

    description: The requested resource (Servlet Survey is not available) is not available.
    --------------------------------------------------------------------------------------------

    ora vi posto in ordine la pagina Html di partenza, la servlet richiamata dalla pagina html, e il file web.xml. Provate a vedere un pochino voi se riuscite a darmi una mano...


    1)index.html
    codice:
    <!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=ISO-8859-1">
    <title>Survey</title>
    </head>
    <body>
    	<form action="Survey" method = "post" >
    	
    
    what is your favourite pet?</p>
    	
    
    
    		<input type = "radio" name = "animal"
    			value ="1" /> Dog 
    
    			
    		<input type = "radio" name = "animal"
    			value ="2" /> Cat 
    
    		
    		<input type = "radio" name = "animal"
    			value ="3" /> Bird 
    
    			
    		<input type = "radio" name = "animal"
    			value ="4" /> Snake 
    
    			
    		<input type = "radio" name = "animal"
    			value = "5" checked = "checked" />None
    			
    		</p>
    		
    		
    
    <input type = "submit" value= "Submit" /> </p>
    		
    
    </form>
    
    </body>
    </html>

    2)Survey.java
    codice:
    package control;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.UnavailableException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.text.*;
    import java.sql.*;
    /**
     * Servlet implementation class for Servlet: Survey
     *
     */
     public class Survey extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
        /* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#HttpServlet()
    	 */
    	 private Connection connection;
    	 private PreparedStatement updateVotes, totalVotes, results;
    	public Survey() {
    		super();
    	}   
    public void init(ServletConfig config)throws ServletException{
    		
    		try{
    			Class.forName("com.mysql.jdbc.Driver");
    			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/animalsurvey","root","1234");
    			updateVotes = connection.prepareStatement("UPDATE surveyresults SET votes = votes + 1 " + "           ");
    			totalVotes = connection.prepareStatement("SELECT  sum (votes) FROM surveyresults");
    			results = connection.prepareStatement("SELECT surveyoption, votes, id" + "FROM surveyresults ORDER BY id");
    		}
    		
    		catch (Exception exception){
    			exception.printStackTrace();
    			throw new UnavailableException (exception.getMessage());
    		}
    	}
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		this.doPost(request, response);
    	}  	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		DecimalFormat twoDigits = new DecimalFormat ("0.00");
    		
    		out.println("<html><head>");
    		int value = Integer.parseInt(request.getParameter("animal"));
    		
    		try{
    			updateVotes.setInt(1, value);
    			updateVotes.executeUpdate();
    			
    			ResultSet totalRS = totalVotes.executeQuery();
    			totalRS.next();
    			int total = totalRS.getInt(1);
    			
    			ResultSet resultsRS = results.executeQuery();
    			out.println("<title>Thank you!</title></head><body>");
    			out.println("
    
    Thank you for partecipating.");
    			out.println("
     Result:</p>");
    			
    			int votes;
    			while(resultsRS.next()){
    				out.print(resultsRS.getString(1)+":");
    				votes = resultsRS.getInt(2);
    				out.print(twoDigits.format((double)votes/total*100));
    				out.print("% responses:" + votes);
    				
    			}
    			resultsRS.close();
    			out.print("Total responses:" +total + "</body></html>");
    			out.close();
    		}
    		
    		catch(SQLException sqlException){
    			sqlException.printStackTrace();
    			out.println("<title>Error</title></head>");
    			out.println("<body>
    
    Database error occured.");
    			out.println("Try again later.</p></body></html>");
    			out.close();
    		}
    	}   	  	    
    	
    	public void destroy(){
    		try{
    			updateVotes.close();
    			totalVotes.close();
    			results.close();
    			connection.close();
    		}
    		catch(SQLException sqlException){sqlException.printStackTrace();}
    	}
    	   	  	    
    }
    3)web.xml
    codice:
      <?xml version="1.0" encoding="UTF-8" ?> 
    - <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <display-name>TestFinal</display-name> 
    - <servlet>
      <description /> 
      <display-name>Survey</display-name> 
      <servlet-name>Survey</servlet-name> 
      <servlet-class>control.Survey</servlet-class> 
      </servlet>
    - <servlet-mapping>
      <servlet-name>Survey</servlet-name> 
      <url-pattern>/Survey</url-pattern> 
      </servlet-mapping>
    - <welcome-file-list>
      <welcome-file>index.html</welcome-file> 
      <welcome-file>index.htm</welcome-file> 
      <welcome-file>index.jsp</welcome-file> 
      <welcome-file>default.html</welcome-file> 
      <welcome-file>default.htm</welcome-file> 
      <welcome-file>default.jsp</welcome-file> 
      </welcome-file-list>
      </web-app>
    credo proprio che il problema stia nel richiamare dalla form la servlet... ho provato anche a mettere come come assegnamento per l'action ="/Survey" ="/NomeApplicazione/control/Survey" ... insomma ho provato in vari modi ma l'errore che mi dà è sempre quello...
    Tomcat è funzionante, tanto che ho provato un'altra applicazione che ho scritto sempre io e non mi dà alcun problema del genere....
    sono nelle vostre mani...
    thank you guys!

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    230
    se non sbaglio nell' action dovresti indicare il path assoluto
    action="http://localhost/Nomeapplicazione/servlet/Survey" > oppure:
    "http://localhost/Nomeapplicazione/Survey" >
    hai provato in questa maniera?

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Secondo me il problema sta nella init() della servlet, c'e' qualcosa che non va a buon fine e la servlet non si avvia. Se provi a commentare il metodo init, la pagina parte o meglio, da un altro errore (un NullPointerException quando il corpo del doPost tenta di accedere ad una delle prepared statement non iniizalizzate), pero' nel doPost ci arriva. Nella action delform puoi mettere sia l'url assoluto come ti ha detto puntino, sia quello relativo come avevi fatto tu; consiglio quest'ultimo per motivi di portabilita'.
    Prova a mettere questo nel try della init().
    codice:
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/animalsurvey","root","1234");
    updateVotes = connection.prepareStatement("UPDATE surveyresults SET votes = votes + 1"); //C'era una serie di spazi che non credo diano fastidio, ma per sicurezza...
    totalVotes = connection.prepareStatement("SELECT  sum (votes) FROM surveyresults");
    results = connection.prepareStatement("SELECT surveyoption, votes, id FROM surveyresults ORDER BY id"); //Mancava lo spazio tra id e from

  4. #4

    ..nuova pagina di errore

    ...intanto vi ringrazio x gli aiuti che sicuramente mi sono serviti....però ora avendo messo le correzioni nella init mi esce un errore completamente diverso.... ora prova a caricare la pagina ma poi mi esce la pagina di errore di FireFox che mi dice "Errore caricamento pagina" e sotto dice "FireFox non può stabilire una connessione con il server localhost"....

    L'url a cui cerca di connettersi è questo:
    http://localhost/TestFinal/servlet/Survey
    (TestFinal è il nome del progetto)

    grazie x l'aiuto!

    p.s.: nell'ultimo post dicevi di commentare il metodo init().....ma in che senso??

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Dicevo nel senso di metterlo tutto tra /* e */ per far modo che java non lo veda (come se non ci fosse il metodo init()). Ripeto, questo non ti risolve il problema, tuttaltro, ma era per farti vedere che il problema sta nella init.
    codice:
    /*
    public void init(ServletConfig config)throws ServletException{
    		
    		try{
    			Class.forName("com.mysql.jdbc.Driver");
                            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/animalsurvey","root","1234");
                            updateVotes = connection.prepareStatement("UPDATE surveyresults SET votes = votes + 1"); 
                            totalVotes = connection.prepareStatement("SELECT  sum (votes) FROM surveyresults");
                            results = connection.prepareStatement("SELECT surveyoption, votes, id FROM surveyresults ORDER BY id"); 
    		}
    		
    		catch (Exception exception){
    			exception.printStackTrace();
    			throw new UnavailableException (exception.getMessage());
    		}
    	}
    */
    Usi Tomcat? Se si, la porta su cui si avvia di default mi pare sia la 8080 quindi l'inidirzzo da mettere e'
    http://localhost:8080/TestFinal/Survey
    se nel file web.xml hai mappato la pagina cosi'
    codice:
    <servlet-mapping>
      <servlet-name>Survey</servlet-name> 
      <url-pattern>/Survey</url-pattern> 
    </servlet-mapping>
    Se vuoi chiamarla dall'indirizzo servlet/Survey devi mattere /servlet/Survey nel file di mapping.
    Cmq meglio gli url relativi, altrimenti l'applicazione ti funziona sulla tua macchina, ma quando la metti online con l'indirizzo non locale, non ti va piu'.
    Quindi l'indirizzo da chiamare e' semplicemente
    codice:
    <form action="Survey" method = "post" >

  6. #6

    ..tutto come prima..

    ...ora è tornato al vecchio errore.... dice che la servlet è 'not available'...
    e il percorso che segue è:
    http://localhost:8080/TestFinal/Survey

    ..non ho però ancora provato i commenti...
    che nervi!!!! è tutto il giorno che provo....

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Beh, semplicemente la servlet non si avvia perche' l'init() solleva un eccezione. Ricontrolla i tuoi statement e poi se non c'e' un motivo ben preciso, li puoi creare direttamente dentro il doPost.

  8. #8

    tento...

    ...sì ora tento qualche altra soluzione x poter identificare bene l'errore...
    cmq ti ringrazio simo x l'aiuto... magari domani riprovo e se ho ancora problems ti riposto qlche domandina!!!

    ciao!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.