1. the jsp code is as follows:
Copy codeThe Code is 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">
<Html>
<Head>
<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>
</Head>
<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> NO. </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" pai.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>
</Html>
2. Access the action Code as follows:
Copy codeThe Code is 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: