1.jsp code as follows
Copy Code code as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" src= "<%=basepath%>js/jquery-1.8.1.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#getResult"). Click (function () {
$.ajax ({
Type: "Post",
URL: "<%=basepath%>jsonaction!getdata.action",
DataType: "JSON",
data:{' param1 ': $ ("#param1"). attr ("value"), ' param2 ': $ ("#param2"). attr ("value")},
Success:function (returndata) {
var html = "<table border= ' 1 ' ><tr><td> number </td><td> name </td><td> description </td ></tr> ";
for (var i = 0; i < returndata.length; i++) {
HTML + + <tr><td> "+returndata[i].id+" </td><td> "+returndata[i].name+" </td><td > "+returndata[i].description+" </td></tr>;
}
$ ("#result"). HTML (HTML);
}
});
});
});
</script>
<body>
<input type= "text" value= "haha" id= "param1" >
<input type= "text" value= "hehe" id= "param2" >
<div id= "Result" ></div>
<input type= "button" value= "Get" id= "GetResult" >
</body>
2. Access action Code as follows
Copy Code code as follows:
public class Jsonaction extends actionsupport{
public void GetData () throws IOException
{
HttpServletRequest req = Servletactioncontext.getrequest ();
String P1 = Req.getparameter ("param1");
String P2 = req.getparameter ("param2");
HttpServletResponse rep = Servletactioncontext.getresponse ();
Rep.setcontenttype ("Text/json;charset=utf-8");
PrintWriter pw = Rep.getwriter ();
String data = "[{\ id\": \ "01\", \ "name\": \ "zhongqian\", \ "description\": \ "+p1+" \ "},{\" id\ ": \" 02\ ", \" name\ ": \" Zhangsan\ ", \" description\ ": \" "+p2+" \ "}]";
Pw.print (data);
Pw.flush ();
}
}
3. The effect is as follows: