前台JSP:
var XMLHTTP;
function creatXMLHttpRequest(){//判断浏览器
if(window.XMLHttpRequest){//一般浏览器
XMLHTTP=new XMLHttpRequest;
}elseif(window.ActiveObject){//ie浏览器
XMLHTTP= newActiveObject("Microsoft.XMLHTTP");}}
function testa() {
var username=document.getElementById("username").value;
creatXMLHttpRequest();//通过js向后台发送请求
XMLHTTP.onreadystatechange=callback;//注册并回调方法
XMLHTTP.open("GET","testAjax?username="+username, true);//get发送方式
XMLHTTP.send(null);}
function callback() {//必须正常响应
//判断http是否成功交互
if (XMLHTTP.readyState==4&&XMLHTTP.status==200){ var text=XMLHTTP.responseText;
varshow=eval("("+text+")");
//show是servicelet传来的list集合
document.getElementById("yxkc").innerHTML="课程编号:"+show.kcid+"课程:"}}
action:
public String wdkc(){
try {
HttpServletResponse response =ServletActionContext.getResponse();
String kc="";
String goodname=newString(wdgoods.getGoodname().getBytes("ISO-8859-1"),"utf-8");
String color=newString(wdgoods.getColor().getBytes("ISO-8859-1"),"utf-8");
System.out.println(color);
System.out.println(goodname);
System.out.println(wdgoods.getStyle());
List l=dao.findwdkc(goodname,wdgoods.getStyle(), color);
for (int i = 0; i < l.size();i++) {
Wdgoods wd=(Wdgoods) l.get(i);
kc=wd.getKucun();
}
System.out.println(kc);
response.getWriter().write(kc);
response.getWriter().flush();
response.getWriter().close();
} catch(UnsupportedEncodingException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
return "findkc";
}