i tested the php version of the autocomplete example in ajax using PHP from here:
http://www.w3schools.com/ajax/ajax_aspphp.asp
i tried the PHP veriosn, not the ASP version. It worked fine.
Now i want to implement the same thing in JSP, and so i translated the PHP version of source file (which has the 'suggestions' ) to JSP to my best effort
but still it doesn't work. i type in some letters, single only though just for testing, and no suggestion comes up, and when i directly run the JSP file, i get some errors
here is the ajaxexample.html code:
<html>
<head>
<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.jsp?q="+str,true);
xmlhttp.send();
}
</script>
</head
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)" size="20" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
here is the JSP version of the PHP code you can see on the above link
<%@ page language="Java" %>
<%!
String hint="";
String q;
String a[]={"AstonMartin","BobDylan","Catherine Zeta Jones", "Dog", "Eerie", "Faad" , "Great" , "Handsome" ,"Idiot" ,"Jasmine" ,"Kaminey" };
String responsse; // this will come up as suggestions
int i=0;
%>
<%
// Fill up array with names
q=request.getParameter("q");
if (q.length > 0)
{
hint="";
for(i=0; i<11;i++)
{
if (q.toLowerCase().equals( a[i].substring(0,q.length).toLowerCase() ) )
{
if (hint.equals(""))
{
hint= a[i];
}
else
{
hint= hint+" , "+ a[i];
} //if else block
} //if after for loop
}//for loop
// } //outermost if
// Set output to "no suggestion" if no hint were found
// or to the correct values
if (hint.equals(""))
{
responsse="no suggestion";
}
else
{
responsse= hint;
}
//output the responsse
out.print(responsse);
%>
the errors i'm getting when i directly run the JSP :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /ajaxexample/exp/gethint.jsp at line 27
24: hint="";
25: for(i=0; i<11;i++)
26: {
27: if (q.toLowerCase().equals( a.substring(0,0).toLowerCase() ) ) //q.length
28: {
29: if (hint.equals(""))
30: {
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root causejava.lang.NullPointerException
org.apache.jsp.ajaxexample.exp.gethint_jsp._jspService(gethint_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
so tell me what is wrong in it or whether it is even possible to do this with JSP. i have to do this in JSP because in my project i need to provide suggestions in the search box,which needs to be simple and understandable