Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get ajax response in jsp page

 
sudarshan jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone , i have a problem in jsp. my getuser.jsp pass a value through ajax call to combo.jsp but emp_id value comming in text but showing null pass to jsp page...... why???
My code is as follow:



combo.jsp


<%@ page import="java.sql.*" %>
<html>
<head>
<style>
A:hover {text-decoration: none;

border: 0px;
font-size:14pt;
color: #2d2b2b; }
</style>

<link rel="stylesheet" type="text/css" href="datepicker.css"/>
<script type="text/javascript">
function showEmp(emp_value)
{
if(document.getElementById("emp_id").value!="-1")
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.jsp"
url=url+"?emp_id="+emp_value

xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

}
else
{
alert("Please Select Employee Id");
}
}

function stateChanged()
{
document.getElementById("ename").value ="";
document.getElementById("emp_id").value ="";
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
var showdata = xmlHttp.responseText;

var strar = showdata.split(":");

if(strar.length==1)
{
document.getElementById("emp_id").focus();
alert("Please Select Employee Id");
document.getElementById("ename").value =" ";
document.getElementById("emp_id").value =" ";

}
else if(strar.length>1)
{

// document.write(strname);
document.getElementById("emp_id").value= strar[1];
document.getElementById("ename").value= strar[2];

}
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>

</head>
<body>
<form name="employee">
<br><br>
<table border="0" width="400px" align="center" bgcolor="#CDFFFF">
<div id="mydiv"></div>
<tr><td><b>Select Employee Id</b></td><td>
<select name="semp_id" onchange="showEmp(this.value);">
<option value="-1">Select</option>
<%

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "table";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";

int sumcount=0;
Statement st;
try {
Class.forName(driver).newInstance();

conn = DriverManager.getConnection(url+dbName,userName,password);
String query = "select * from station";

st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
%>

<%
while(rs.next())
{
%>

<option value="<%=rs.getString(1)%>"><%=rs.getString(3)%></option>

<%
}
%>

<%

}
catch (Exception e) {
e.printStackTrace();
}
%>
</select>
<%
String sid=request.getParameter("emp_id");
System.out.println(sid);
%>

</td></tr>
<tr><td ><b>Employee Id:</b></td><td>
<input type="text" name="emp_id" id="emp_id" value=""></td></tr>
<tr><td><b>Employee Name:</b></td><td>
<input type="text" name="emp_name" id="ename" value=""></td></tr>
</table>


</form>
<table border="0" width="100%" align="center">
<br>
<br>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------
getuser.jsp


<%@ page import="java.sql.*" %>
<%
String emp_id = request.getParameter("emp_id").toString();
String data ="";
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "table";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
int sumcount=0;
Statement st;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
String query = "select * from station where sid='"+emp_id+"'";
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
data = ":" + rs.getString(1) + " : " + rs.getString(2) ;
}
out.println(data);
}
catch (Exception e) {
e.printStackTrace();
}
%>
 
Pongsakorn Semsuwan
Ranch Hand
Posts: 34
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, use code tag when you include any code in your post.

I suggest you to alert your url before making a call so that you can know what you really call.
And then you might want to try calling getuser.jsp manually with emp_id passed
 
subhod lagade
Greenhorn
Posts: 6
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i can sugest is you can debug the response page by passing query string to the response page and check whether you get the result there...........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic