• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using AJAX in Struts.... need steps to Implement..

 
Harish Maridi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I am new to java, I am trying to implement AJAX with struts framework.
Please let me know the steps to be followed to do that.

What I did is,

step 1: I attached a listner to the JSP dropdown menu field
step 2: I made a java script file with url: db.jsp
step 3: created a db.jsp, and accessed the parameter from the jsp here
step 4: I catched the response of the server in java script file

Here comes the problem, I am unable to populate the list from the server responce in to the JSP drop down menu list.

I am also providing the code that I did....

JSP code where I am checking the State change:
Name: EnterNewRequest.jsp

<script type="text/javascript" src="select.js">

</script>

<select name="state" onchange="show(this.value)">


This is my java script for AJAX call� Observe the FUNCTION stateChanged()�.
Name: select.js

var xmlHttp

function show(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="DB.jsp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{

alert(xmlHttp.responseText)
document.getElementById("list")=xmlHttp.responseText;




}
}

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;
}

This is my DB.Jsp from where the data from DB is fetching�..




<%
ArrayList COCLLIList = new ArrayList();
EnterNewRequestFormENRF = new EnterNewRequestForm();

Connection conn = Connection.getConnection();


PreparedStatement pstmt = null;
ResultSet rs = null;

String COCLLIsql = "SELECT LOC "+"FROM FINAL"+" WHERE LOC like '%"+request.getParameter("q")+"%'";
System.out.println("SQL :"+sql);
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
List = new ArrayList();

while (rs.next()){

String loc = rs.getString(1);
List.add(loc);
}


out.println(List);


rs.close();
pstmt.close();

}catch (Exception e){
System.out.println(e.getMessage());
throw e;
}finally{
if(rs!=null){try{rs.close();}catch(SQLException e){}}
if(pstmt!=null){try{pstmt.close();}catch(SQLException e){}}
if(conn!=null){try{conn.close();}catch(SQLException e){}}
}


if(List.isEmpty()){
System.out.println("ArrayList is Emplty");
}


%>

The List is coming here��.

The section where I need to put that CLLIList in JSP is �..
Name: EnterNewRequest.jsp

<select id="list" name="second">
<option selected value="0">Choose</option><option value="1"><div id="cllilist"><b></b></div>
</option> <option value="2">No Values</option></select></td>


This is the process I followed, but I am not getting the data to this dropdown menu.

My frame work is Struts, but here I am operating on db.jsp which is not encourageble, Please suggest me how to do it with struts.

any help will be appreciated....
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mark_watson",
Please check your private messages regarding an important administrative matter.
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic