Expand|Select|Wrap|Line Numbers
- <script language="javascript">
- // Global Variable for XmlHttp Request Object
- var xmlhttp;
- var yearDDL;
- function handleOnChangepType(ddl)
- {
- //index y texto del item seleccionado
- var ddlIndex = ddl.selectedIndex;
- var chosenType = ddl[ddlIndex].text;
- var frmSelect = document.forms["frmSelect"];
- var frmSelectElem = frmSelect.elements;
- yearDDL = frmSelectElem["year"];
- if ( chosenType != "Select Type")
- {
- xmlhttp = null
- // code for initializing XmlHttpRequest Object On Browsers like Mozilla, etc.
- if (window.XMLHttpRequest){
- xmlhttp = new XMLHttpRequest()
- }
- // code for initializing XmlHttpRequest Object On Browsers like IE
- else if (window.ActiveXObject) {
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
- }
- if (xmlhttp != null){
- // Setting the Servlet url to get XmlData
- url = "searchAvailableYears.jsp?pType=" + chosenType;
- // Course of Action That Should be Made if their is a change in XmlHttpRequest Object ReadyState NOTE : it is 4 when it has got request from CGI
- xmlhttp.onreadystatechange = handleHttpResponse;
- // Open the Request by passing Type of Request & CGI URL
- xmlhttp.open("GET",url,true);
- // Sending URL Encoded Data
- xmlhttp.send(null);
- }
- else{
- // Only Broswers like IE 5.0,Mozilla & all other browser which support XML data Supports AJAX Technology
- // In the Below case it looks as if the browser is not compatiable
- alert("Your browser does not support XMLHTTP.")
- } //else
- } //if chosen
- } //fucntion
- //----------------------------
- /* Used for verifing right ReadyState & Status of XmlHttpRequest Object returns true if it is verified */
- function verifyReadyState(obj){
- // As Said above if XmlHttp.ReadyState == 4 then the Page Has got Response from WebServer
- if(obj.readyState == 4){
- // Similarly if XmlHttp.status == 200 it means that we have got a Valid response from the WebServer
- if(obj.status == 200){
- return true
- }
- else{
- alert("Problem retrieving XML data")
- }
- }
- }
- //-----------------------------------
- function handleHttpResponse()
- {
- if(verifyReadyState(xmlhttp) == true)
- {
- // Building a DOM parser from Response Object
- var x = xmlhttp.responseXML.getElementsByTagName("option");
- // Checking for the Root Node Tag
- // var x = response.split("|")
- // var tex
- // var optn
- yearDDL.length = 1;
- yearDDL.length = x.length;
- for (o=1; o < x.length; o++)
- {
- yearDDL[o].text = x[o];
- }
- else
- {
- alert("Cannot handle the Ajax call.");
- }
- } //if
- }//function
- </script>
HTML part:
[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="ivory">
<form name="frmSelect">
<p><strong><u>Test</u></strong></p>
<p><hr color="orange"></p>
<table border="0">
<tr><td><strong>Type</strong></td>
<td>
<select name="pType" onchange="handleOnChangepType(this);">
<option>Select Type</option>
<%
String allDir[] = {"A", "B", "C"}; //dirs con los que se trabajará
for(int i=0; i<3; i++)
{
File dir = new File("C:/apache-tomcat-6.0.10/webapps/ROOT/p1/db/" + allDir[i] ); //(fenomenos A, B, C)
if (dir.exists() && dir.isDirectory())
{
%>
<option><%=allDir[i]%></option>
<%
} //if
} //for
%>
</select >
</td></tr>
<td><strong>Year</strong></td>
<td>
<select name="year">
<option></option>
</select>
</td>
</tr>
<tr><td><strong>Day</strong></td>
<td>
<select name="day">
<option></option>
</select>
</td></tr>
<tr><td><strong>Times available<br>for the selected day</strong></td>
<td>
<select name="range1">
<option></option>
</select>
</td>
</tr>
</td></tr>
</table>
</body>
</html>[/HTML]---------------------------------------------------------
The JSP code:
Expand|Select|Wrap|Line Numbers
- <%@ page contentType="text/html" %>
- <%@page import="java.io.*,
- javax.servlet.*,
- javax.servlet.http.*,
- java.lang.*,
- java.util.*
- "
- %>
- <%
- String pType = request.getParameter("pType");
- response.setContentType("text/xml");
- response.setHeader("Cache-Control", "no-cache");
- if (pType == "A" || pType == "B" || pType == "C")
- {
- File dir = new File("C:/apache-tomcat-6.0.10/webapps/ROOT/p1/db/" + pType);
- String[] allFilesInDir = dir.list();
- ArrayList years = new ArrayList();
- String option;
- for(int i=0; i < allFilesInDir.length; i++)
- {
- if (allFilesInDir[i].substring(0,0) == "d" || allFilesInDir[i].substring(0,0) == "D")
- {
- years.add(allFilesInDir[i].substring(1,4));
- option = "<option>" + years.get(i) + "</option>";
- response.getWriter().write(option);
- }
- }
- }
- else
- {
- response.getWriter().write("<option>THIS IS A TEST</option>");
- }
- %>
Thank you!