Código HTML:
Ver original
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="css/estilo.css" type="text/css"> </head> <body onload="cargarDatos()"> <div id="seleccion"> <table> <tbody> <tr> <td>Origen <select name="origen" id="origen"> <% ArrayList listaAeropuertos=(ArrayList)request.getSession().getAttribute("listaAeropuertos"); for(int i=0;i<listaAeropuertos.size();i++){ %> <% } %> </select> </td> <td>Destino <select name="destino"> </select> </td> </tr> <tr> </tr> <tr> <td>Adultos <select name="n_adultos"> </select> </td> <td>Niños <select name="n_niños"> </select> </td> <td>Bebes <select name="n_bebes"> </select> </td> </tr> </tbody> </table> <form action="ServletOrigen" name=""> </form> <%!ConexionBBDD ConexBD;%> <%!public void jspInit(){ try{ ConexBD=ConexionBBDD.GetConexion(); }catch(SQLException e){ }catch(ClassNotFoundException ce){ } } %> <%!public void jspDestroy(){ try{ ConexBD.Destroy(); }catch(SQLException e){ } } %> </div> </body> </html>
Los datos los quiero cargar en el select de name="origen". Para ello estoy usando la funcion cargarDatos() que se encuentra en el siguiente fichero javascript
Código Javascript:
Ver original
var objAjax; function AJAXCrearObjeto(){ if(window.XMLHttpRequest){ objetoAjax=new XMLHttpRequest(); } else{ objetoAjax=new ActiveXObject("Microsoft.XMLHTTP"); } return objetoAjax; } function cargarDatos(){ objAjax=new AJAXCrearObjeto(); objAjax.open('POST','http://localhost:8084/LineasAereas/ServletOrigen',true); objAjax.send(); objAjax.onreadystatechange=respuestaCarga; } function respuestaCarga(){ if(objAjax.readyState==4 && objAjax.status==200){ alert("responseText:"+objAjax.responseText); var miArray=objAjax.responseText; mostrarDatos(miArray); } } function mostrarDatos(miArray){ var array_datos=JSON.parse(miArray); var origen=document.getElementById("opt_origen"); for(var i in array_datos){ origen.innerHTML=array_datos[i].NOMBRE; } }
Desde la funcion que he dicho antes llamo al servlet:
Código Java:
Ver original
public class ServletOrigen extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override public void init()throws ServletException{ try{ ConexionBBDD conexBD=ConexionBBDD.GetConexion(); conexion=conexBD.GetCon(); } } protected void processRequest(HttpServletRequest request, HttpServletResponse response) response.setContentType("text/html;charset=UTF-8"); try{ listaAeropuertos=new Aeropuertos().getAeropuertos(); HttpSession session=request.getSession(true); session.setAttribute("listaAeropuertos", listaAeropuertos); response.sendRedirect("index.jsp"); out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet ServletOrigen</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Lista de aeropuertos " + listaAeropuertos.get(0) + "</h1>"); out.println("</body>"); out.println("</html>"); }finally{ out.close(); } }
El caso es que el select no me carga ningun dato y no se porque puede ser. La verdad es que estoy bastante perdido en esto y necesito ayuda. Gracias
Se que este tema puede que no vaya aqui pero es que no me dejaba subirlo a otro sitio