Salve a tutti,
Da un po' di giorni sto studiando come creare i servlet e, usando programmi già fatti su internet come spunto, penso di aver capito le basi.
Il problema è che mi da problemi quando da "index.jsp" devo passare al servlet "Controllo.java". In "web.xml" ho già inserito tutto quello che serve e non ci sono errori di scrittura...
Codici:
index.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 http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Iscrizione</title>
</head>
<body>
<div style="text-align:center">
<form action="Controllo" name="Controllo">
Nome: <input name="nome" type="text"/><br>
Cognome: <input name="cognome" type="text"/><br>
Nazione: <input name="nazione" type="text"/><br>
<input name="Show" type="submit" value="Vai"/>
</form>
</div>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>UtentiWeb</display-name><servlet>
<servlet-name>Controllo</servlet-name>
<servlet-class>Controllo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controllo</servlet-name>
<url-pattern>/Controllo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Controllo.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
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 java.util.Vector;
/**
* Servlet implementation class Controllo
*/
@WebServlet("/Controllo")
public class Controllo extends HttpServlet {
private static final long serialVersionUID = 1L;
private Vector<Utente>utenti;
/**
* @see HttpServlet#HttpServlet()
*/
public Controllo() {
utenti=new Vector<>();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
if(request.getParameter("nome").isEmpty()){
out.println("<html>"+
"<title>Errore 001</title>"+
"<body>"+
"<h1>Errore 001: Nome non inserito!</h1>"+"<br>"+
"<a href=\"index.jsp\"><input type=\"submit\" value=\"Indietro\"/></a>"+
"</body></html>");
}else{
String nome=request.getParameter("nome");
if(request.getParameter("cognome").isEmpty()){
out.println("<html>"+
"<title>Errore 002</title>"+
"<body>"+
"<h1>Errore 002: Cognome non inserito!</h1>"+"<br>"+
"<a href=\"index.jsp\"><input type=\"submit\" value=\"Indietro\"/></a>"+
"</body></html>");
}else{
String cognome=request.getParameter("cognome");
if(request.getParameter("nazione").isEmpty()){
out.println("<html>"+
"<title>Errore 003</title>"+
"<body>"+
"<h1>Errore 003: Nazione non inserita!</h1>"+"<br>"+
"<a href=\"index.jsp\"><input type=\"submit\" value=\"Indietro\"/></a>"+
"</body></html>");
}else{
String nazione=request.getParameter("nazione");
utenti.add(new Utente(nome,cognome,nazione));
String page="<html><title>Elenco Utenti</title><h1>Utenti:</h1><br>";
for(int i=0; i<utenti.size(); i++){
page+="<p>Utente:</p><br><h4>Nome: "+utenti.get(i).getNome()+"</h4><br>"+
"<h4>Cognome: "+utenti.get(i).getCognome()+"</h4><br>"+
"<h4>Nazione: "+utenti.get(i).getNazione()+"</h4><br>";
}
}
}
}
}
/**
* @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);
}
}
Errore riportato:
javax.servlet.ServletException: Error instantiating servlet class Controllo
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.ClassNotFoundException: Controllo
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
Grazie a tutti in anticipo,
Niar