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
3)web.xmlcodice: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();} } }
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...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>
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!

Rispondi quotando