Ciao a tutti:
lo so che sto rompendo tanto con le mie richieste. Però effettivamente è un argomento che mi piace molto e sto cercando di impararlo al meglio.
Prendendo spunto da questo thread:
http://forum.html.it/forum/showthread/t-1130792.html
ho sistemato il codice e l'ho settato per il mio lavoro.
Ho creato una servlet SelectStruttura:
Codice PHP:
package it.SelectStruttura;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import it.MiaClasseDb.*;
public class SelectStruttura extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
int struct;
ResultSet rs = null;
String responseText = "";
public void init(ServletConfig config) throws ServletException {
// Always call super.init(config) first (servlet mantra #1)
super.init(config);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
struct = Integer.parseInt(req.getParameter("struttura"));
try {
DatabaseConnection connection = new HsqldbConnection("PRONTOEPULITO");
Statement stm = connection.createStatement();
String sql = "select distinct dbo.CLIID00F.POSI00 AS AREA FROM dbo.CLIID00F "
+ "where dbo.CLIID00f.CMPNUM=" + struct;
System.out.println(sql);
stm.execute(sql);
rs = stm.getResultSet();
responseText = "<select name=\"area\" id=\"l2\" >";
if (rs != null) {// if rs == null, then there is no ResultSet to view
while (rs.next()) { // this will step through our data row-by-row
responseText = responseText
+ "<option value=\"" + rs.getString("AREA") + "\">" + rs.getString("AREA") + "</option>";
}
}
responseText = responseText + "</select>";
stm.close(); // close the Statement to let the database know we're done with it
connection.close(); // close the Connection to let the database know we're done with it
System.out.println(responseText);
out.println(responseText);
out.close();
} catch (Exception e) {
System.out.println("ERROR: " + e);
}
}
public void destroy() {
}
}
E poi ho creato il mio file jsp:
Codice PHP:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="it.MiaClasseDb.DatabaseConnection" %>
<%@ page import="it.MiaClasseDb.HsqldbConnection" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
List strut = new ArrayList();
try {
DatabaseConnection connection = new HsqldbConnection("PRONTOEPULITO");
Statement stm = connection.createStatement();
if (struttura != 0) {
sql = "SELECT dbo.HOSID00F.CMPNUM AS Codice, dbo.HOSID00F.HOSPDE As Struttura "
+ "FROM dbo.HOSID00F WHERE dbo.HOSID00F.CMPNUM=" + struttura + " "
+ "UNION ALL "
+ "SELECT dbo.HOSID00F.CMPNUM AS Codice, dbo.HOSID00F.HOSPDE As Struttura "
+ "FROM dbo.HOSID00F";
} else {
sql = "SELECT dbo.HOSID00F.CMPNUM AS Codice, dbo.HOSID00F.HOSPDE As Struttura "
+ "FROM dbo.HOSID00F";
}
ResultSet rs = stm.executeQuery(sql);
if (rs != null) {
while (rs.next()) {
strut.add(rs.getInt("Codice"));
strut.add(rs.getString("Struttura"));
}
}
stm.close();
// close the database connection
connection.close();
} catch (Exception cnfex) {
cnfex.printStackTrace();
out.println(cnfex);
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" title="currentStyle">
@import "Styles/prontoepulitostyle.css";
</style>
<script type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script language="javascript" type="text/javascript">
<!--
var http = createRequestObject();
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function addElement(ogg,val,text){
var newOption;
newOption = document.createElement("option");
newOption.value = val;
newOption.text = text;
ogg.add(newOption, where);
}
function removeElement(ogg){
if(ogg.options.length > 0){
while (ogg.options.length) {
ogg.remove(0);
}
}
}
function disabledElement(ogg,val){
document.getElementById(ogg).disabled = val;
}
function loadingStruttura(oggId){
var id = document.getElementById(oggId).value;
http.open('GET','do.getArea?prov='+id, true);
http.onreadystatechange = getArea;
http.send(null);
}
function getArea(){
var State = document.getElementById('l2');
removeElement(State);
id = document.getElementById('Select_struttura').value;
if(id == 'sel'){
disabledElement('l2',true);
addElement(State,'sel','Seleziona prima la struttura:');
}else{
if(http.readyState == 4){
if (http.status == 200) {
var response = http.responseText;
document.getElementById("select2").innerHTML = response;
disabledElement('l2',false);
}
}else{
addElement(State,'sel','Loading...');
}
}
}
//-->
</script>
</head>
<body>
<h2>Generatore Report</h2>
<form action="generaReport.jsp" name="form" method="get">
<div id="content"
<table class="table">
<tr class="tr">
<td>[b]Struttura:[/b]</td>
<td>
<div id="select1">
<select id="Select_provincia" name="Select_struttura" onchange="loadingStruttura('Select_struttura')" >
<%for (int i = 0; i < strut.size(); i = i + 2) {%>
<option value="<%=strut.get(i)%>"><%=strut.get(i + 1)%></option>
<% }%>
</select>
</div>
</td>
</tr>
<tr class="tr">
<td>[b]Area:[/b]</td>
<td>
<div id="select2"
<select name="l2" id="l2">
<option value="sel" selected="selected">Seleziona un'area:</option>
</select>
</div>
</td>
</tr>
</table>
</div>
</form>
</html>
Il problema è che non funziona... Mi esegue la prima select elencandomi i risultati richiesti, ma la seconda select non si attiva mai. Rimane sempre l'option Seleziona prima la struttura...