HI I am trying to pass a value from dropdown list on onchange event fo the dropdown in a jsp page through AJAX to servlet. The servlet then returns the value back to the jsp page and displays it in dropdown list.
the problem I am facing is that the value returned is null. though in the servlet the value is set but when I pass the value to jsp, the value is null. somewhere I am passing the value properly. could someone please help. It's important. thnaks in advance.
here is the code for that:
inputPage.jsp
<html>
<head>
</head>
<body>
<%@page language="java" import="java.util.*,java.sql.*,java.util.ArrayList,java.io.*" contentType="text/html" %>
<script type="text/javascript">
function showValues(str)
{
alert("str"+str);
if (str=="")
{
document.getElementById("col3").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)
{
alert("xmlhttp.status"+xmlhttp.status);
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
alert("str1"+str);
xmlhttp.open("GET","/Tester.html?q="+str,true);
xmlhttp.send();
}
</script>
<form name="myform" method="GET">
<div align="left">
<table id="table1" border="1" cellpadding="0" cellspacing="0" width="70%">
<tr>
<th id="header2" width="33%" bgcolor="#969696"><b>TAG NAMES</b></th>
<th id="header3" width="33%" bgcolor="#969696"><b>VALUE</b></th>
</tr>
<tr>
<td id="col2" width="33%"><select name="tagName" onchange="showValues(this.value)">
<option id="row10" name="select" value="1">SELECT</option>
<option id="row11" name="func_type" value="2">FUNCTION_TYPE</option>
<option id="row12" name="comm" value="3">COMMAND</option>
<option id="row13" name="pay_type" value="4">PAYMENT_TYPE</option>
</select>
</td>
<td>
<select name="txtHint">
<%String name=(String)request.getParameter("q");%>
<option ><%=name%></option>;
<option >0</option>;
</select>
<td>
</tr>
</table>
</div>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</form>
</body>
</html>
DataServlet.java
JSP Syntax (Toggle Plain Text)
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
System.out.println("in servlet#########################: doGet() method");
PrintWriter out = response.getWriter();
tagName = request.getParameter("q");
System.out.println("tagName"+tagName);
if(tagName.equals("2")){
System.out.println("tagValue"+tagValue);
tagValue = getInitParameter("2");
System.out.println("tagValue"+tagValue);
request.setAttribute("q",tagValue);
System.out.println("attribute value "+request.getAttribute("q"));
RequestDispatcher dispatcher =
request.getRequestDispatcher("/inputPage.jsp");
if (dispatcher != null) dispatcher.forward(request, response);
}
String tagName = "";
String tagValue ="";
}
web.xml
<servlet>
<servlet-name>
DataServlet
</servlet-name>
<servlet-class>
tester.DataServlet
</servlet-class>
<init-param>
<param-name> debug</param-name>
<param-value>true</param-value>
<!-- <description>Set to 'true' for debug output.</description> -->
</init-param>
<init-param>
<param-name>2</param-name>
<param-value>PAYMENT</param-value>
</init-param>
<init-param>
<param-name>3</param-name>
<param-value>SALE</param-value>
<!--<param-value>DEBIT</param-value> -->
</init-param>
<init-param>
<param-name>4</param-name>
<param-value>CREDIT</param-value>
<!-- <param-value>VOID</param-value>-->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
DataServlet
</servlet-name>
<url-pattern>
/RitaTester.html
</url-pattern>
</servlet-mapping>