Servlet CaricaCombo.java
codice:
package provaprogetto;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CaricaCombo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public CaricaCombo() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Argomento> lista = new ArrayList<Argomento>();
try
{
Connection conn= DriverManager.getConnection("jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Gest_Doc/Database/GestioneDocumenti");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM ARGOMENTI");
while(rs.next())
{
Argomento argomento = new Argomento(rs.getInt("Id_Argomento"), rs.getString("Descrizione"));
lista.add(argomento);
}
request.setAttribute("lista", lista);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("Nuovo.jsp");
dispatcher.forward(request,response);
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage()) ;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
qui richiamo la pagina Nuovo.jsp che è qll dove ho i campi vuoti e la combobox!!!
La pagina Nuovo.jsp ha qst codice:
codice:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:directive.page import="java.util.ArrayList"/>
<jsp:directive.page import="java.sql.*"/>
<jsp:directive.page import="provaprogetto.Argomento"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Nuovo Documento</title>
</head>
<body style="background-color: rgb(215, 215, 215);">
<div style="text-align:center;">
[img]Image\testata_dkb.jpg[/img]
</div>
<table cellspacing="5" cellpadding="5" border="0" align="center" style="margin-top: 120px;" width="150px">
<tr>
<td> Nuovo </td>
<td> Ricerca</td>
</tr>
</table>
<form action="Inserimento" method="get">
<table cellspacing="3" cellpadding="0" border="1" align="center" style="margin-top: 10px;" bgcolor="">
<tr>
<td align="right">Titolo</td>
<td><input id="Titolo" name="Titolo" type="text" value=""/></td>
</tr>
<tr>
<td align="right">Data Pubblicazione</td>
<td><input id="Data_Pubblicazione" name="Data_Pubblicazione" type="text" value=""/></td>
</tr>
<tr>
<td align="right">Argomento</td>
<td>
<select id="Argomento" name= "Argomento" >
<%
ArrayList lista = (ArrayList)request.getAttribute("lista");
for(int i=0;i<lista.size() ;i=i++){
Argomento arg = (Argomento) lista.get(i);
%>
<option value="<%= arg.getId() %>">
<%= arg.getDescrizione() %>
</option>
<%} %>
</select>
</tr>
<tr>
<td><input type="submit" value="Inserisci"></td>
<td><input type="reset" value="Pulisci"></td>
</tr>
</table>
</form>
</body>
</html>
poi la servlet che richiamo su Nuovo.jsp è Inserimento.java
codice:
package provaprogetto;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import provaprogetto.CaricaCombo;
public class Inserimento extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
CaricaCombo io = new CaricaCombo();
static String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
static String DB_Url = "jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Gest_Doc/Database/GestioneDocumenti";
static String titolo;
static String data;
int argomento;
PreparedStatement stmtIns;
Connection conn;
RequestDispatcher dispatcher;
int id;
// ArrayList<Argomento> lista = new ArrayList<Argomento>();
public Inserimento(){
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
titolo = request.getParameter("Titolo");
data = request.getParameter("Data_Pubblicazione");
argomento = request.getIntHeader("Argomento");
try
{
conn = DriverManager.getConnection(DB_Url);
//INSERT
String sql = "INSERT INTO DOCUMENTI(Titolo,Data_Pubblicazione,rid_argomento,path_allegato,rid_utente) VALUES('"+ titolo + "','" + data + " 00.00','3','C/prog','"+ Login.Id_Utente + "')";
stmtIns = conn.prepareStatement(sql);
stmtIns.executeUpdate();
stmtIns.close();
conn.close();
}
//IN TUTTI I CASI
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage()) ;
}
finally{
try{
stmtIns.close();
conn.close();
}
catch(SQLException ex)
{System.out.println(ex.getMessage());}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doGet(request, response);
try {
stmtIns.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
il codice in rosso mi da errore qnd mando in esecuzione la pagina...
Come mi hai detto già precedentemente Simo7784, la lista è null...
ma ke sbaglio?
Ti giuro ke se nn ci riesco gg lascio stare qst benedetto programmai...
:master: