Monday, August 26, 2013

Function With Multiple Values JavaScript+JSP+Servlet

Function With Multiple Values JavaScript+JSP+Servlet

I have a project in Java with JSP+JavaScript+Servlets, with a function in
JavaScript to find some record in my database. When this record is found,
my function returns some value, and when it's not, my function returns a
message. Now I need another function to return several fields from my
database, and I don't know who. I'm not very familiar with JavaScript and
JSP.
This is part from my JSP code:

<tr>
<td>
<input type='text' id="noeco" name="noeco" size=5 maxlength=5
onkeyup="find_noeco_s(noeco,exists);">
<span name="exists" id="exists" readonly="readonly" style="width:
200px" value="{exists}"></span></input>
</td>
</tr>
<tr>
<td>
<input type='text' id="matric" name="matric" readonly="readonly"
value="{matric}"></input>
</td>
</tr>
<tr>
<td>
<input type='text' id="marca" name="marca" readonly="readonly"
value="{marca}"></input>
</td>
</tr>
<tr>
<td>
<input type='text' id="submarca" name="submarca" readonly="readonly"
value="{submarca}"></input>
</td>
</tr>
This is my JavaScript function "find_noeco_s":
function find_noeco_s(noeco,exists){
objsal = exists;
if(window.XMLHttpRequest)
ajax = new XMLHttpRequest();
else
ajax = new ActiveXObject("Microsoft.XMLHTTP");
ajax.onreadystatechange = funcionCallback;
ajax.open("GET", "/processEco.jsp?noeco="+noeco.value, true);
ajax.send("");
}
And this is my JSP processEco:
String noeco = request.getParameter("noeco");
String exists="";
String matri= "";
try{
PreparedStatement ps = PV.prepareStatement("select * from vehicles
where econom=?");
ps.setString(1, noeco);
ResultSet rs = ps.executeQuery();
if(rs.next()){
exists= rs.getString("dstipveh");
}else{
exists= "DOES NOT EXISTS";
}
%>
<%=exists%>
<%
}finally{}
%>
This function returns just one value (exists), but I need to return
several values. I tried to do this:
try{
PreparedStatement ps = PV.prepareStatement("select * from vehicles
where econom=?");
ps.setString(1, noeco);
ResultSet rs = ps.executeQuery();
if(rs.next()){
exists= rs.getString("dstipveh");
matri = rs.getString("matr");
marca= rs.getString("marca");
}else{
exists= "DOES NOT EXISTS";
}
%>
<%=exists%>
<%=matri%>
<%=marca%>
<%
}finally{}
%>
But I get just one value (exists). Like I said, I'm not very familiar with
JavaScript and JSP. So, how can I return several values and display this
values in my JSP like I do with just one value?

No comments:

Post a Comment