运用ajax中,在前端和后台之间进行参数的传递。
在html中主要的代码:
<script type="text/javascript" >
Ext.onReady(function(){
function getMenuAjax(start,end) {
Ext.Ajax.request({
url : './ServletAjax',
method : 'post',
async: false,
params : {
m:start,
n:end
},
success : function(response, options) {
alert(response.responseText);
},
failure : function() {
alert('获取数据失败');
}
});
}
getMenuAjax(0,10);
});
</script>
/**
* Servlet implementation class ServletAjax
*/
//说明这个Servlet是没有序列号的
@SuppressWarnings("serial")
//说明这个Servlet的名称是ServletAjax,其地址是/ServletAjax
//这与在web.xml中设置是一样的
@WebServlet(name = "ServletAjax", urlPatterns = { "/ServletAjax" })
public class ServletAjax extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletAjax() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
request.setCharacterEncoding("utf-8");
String param1=request.getParameter("m");
String param2=request.getParameter("n");
pw.print("前台传来了参数:m="+param1+",n="+param2);
pw.flush();
pw.close();
}
}