wanted: JSP Ajax example, output several "option" elements

M

--MMS--

Hello:

I am like crazy looking for a example, first, of a jsp/servlet which
responds to a series of <option> elements, second, to an Ajax example
getting what the jsp/servlet responds and showing it on drop down box.
I have found many, but for PHP.


Until now I have done this:
<%@ page contentType="text/html" %>
<%@page import="java.io.*, javax.servlet.*,
javax.servlet.http.*, java.lang.*,java.util.*
"
%>
<%

String pType = request.getParameter("pType");

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.substring(0,0) == "d" ||
allFilesInDir.substring(0,0) == "D")
{
years.add(allFilesInDir.substring(1,4));
option = "<option>" + years.get(i) + "</option>";

response.getWriter().write(option);


}

}
}
else
{

response.getWriter().write("<option>THIS IS A TEST</option>");
}

%>
----------------------------------------------

And for the HTML/Javascript/Ajax part I have done the code below,
taking examples from http://www.webmonkey.com/webmonkey/06/15/index3a_page2..html?tw=authoring
BUT I have not yet found the correct solution (it is not giving the
desired output that is, according to what the user selects in the
first drop down box, then the 2nd drop down box will show what the jsp
responds:


<%@ page contentType="text/html" %>
<%@page import="java.io.*, javax.servlet.*,
javax.servlet.http.*,java.lang.*,java.util.*"
%>

<script src="request.js"></script>

<script src="request.js">
//var http = getHTTPObject();
var yearDDL;
var doy;
var time1;

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")
{

Http.get({
url: "searchAvailableYears.jsp?pType=" + chosenType,
callback: handleHttpResponse,
cache: Http.Cache.Get
}, [year]);
}
}


function handleHttpResponse(xmlreply, _yearDDL)
{
if (xmlreply.status == Http.Status.OK)
{

var responseText = xmlreply.responseText;
var items = responseText.split("<\u0022");

_yearDDL.length = 1;
_yearDDL.length = items.length;
for (o=1; o < items.length; o++)
{
_yearDDL[o].text = items[o];
}
}
else
{
alert("Cannot handle the Ajax call.");
}
}


</script>
<html>
<head>
<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 ); //(fenomenos A, B, C)

////////////////////////////////////////////////////////
//String [] allFilesInDir = dir.list();
if (dir.exists() && dir.isDirectory())
{
%>
<option><%=allDir%></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>

ANY HELP IS APPRECIATED!

MMS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top