PDA

View Full Version : [Servlet Java - JSP] Tomcat applicazione web non funziona


forza_sugar
18-02-2013, 19:24
Tomcat applicazione web non funziona
Ciao ragazzi,
Ho iniziato a studiare le servlet java e le applicazioni web. Ho installato tomcat 7.0 ma non riesco a far funzionare le applicazioni con le servlet. Ho seguito la guida html ma dopo i primi due esempi (html + jsp) il terzo non mi funziona.
Ho letto poi, che è necessario il file .class e volevo chiedervi, come faccio a crearlo da quello .java. So che per compilarle il file java e quindi crearlo è necessario il metodo principale main ma nelle servlet non è presente. Ho provato anche ad eseguire le applicazioni da eclipse ma con esiti negativi (da qui ho preso i file .class)
Vi posto i codici presi da un libro e che non riesco a far eseguire.
Servlet formato .class webapps/jhtp5/WEB-INF/classes

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class WelcomeServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<?xml version =\"1.0\"?>");
out.println("<!DOCTYPE html PUBLIC \"-/W3C/DTD XHTML 1.0 Strict//EN" +
"http://www.w3.org/TR/xhtml1/DTDxhtml1-strict.dtd\">");

out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title> Un Semplice Servlet </title>");
out.println("</head>");

out.println("<body>");
out.println("<h1>Welcome to Servlets!</h1>");
out.println("</body>");

out.println("</html>");
}
}

file HTML nella cartella webapps/jhtp5/servlets

<?xml version ="1.0"?>
<!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTDxhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>
<title> Handling an HTTP Get Request </title>
</head>

<body>
<form action = "jhtp5/welcome" method = "get">

<p><label>Clicca sul pulsante per invocare la servlet
<input type = "submit" value = "Get Html Document" />
</label></p>

</form>
</body>
</html>

file web.xml nella cartella webapps/jhtp5/WEB-INF

<!DOCTYPE web-app PUBLIC
"//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dts/web-app_2_2.dtd">

<web-app>

<display-name>
Esempio Servlet
</display-name>

<description>
Questa è l'applicazione Web
</description>

<servlet>
<servlet-name>welcome</servlet-name>
<description>
Un semplice servlet che gestisce una richiesta GET
</description>

<servlet-class>
WelcomeServlet
</servlet-class>

</servlet>



<servlet-mapping>
servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

</web-app>


Grazie per l'attenzione