首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

servlet或struts的Action处置ajax请求

2012-09-04 
servlet或struts的Action处理ajax请求其实处理ajax的请求非常简单,直接看代码就行了://如果用的是struts//

servlet或struts的Action处理ajax请求

其实处理ajax的请求非常简单,直接看代码就行了:

//如果用的是struts//HttpServletResponse response = ServletActionContext.getResponse();// 设置输出为文字流response.setContentType("text/plain");// 设置字符集response.setCharacterEncoding("UTF-8");// 获取原始的PrintWriter对象,以便输出响应结果,而不用跳转到某个视图PrintWriter out = null;//json对象Gson gson = new Gson();try {out = response.getWriter();} catch (IOException e) {return null;}Object o=new Object();out.println(gson.toJson(o));out.flush();out.close();return null;

?

?

?这里如果想返回json格式字符串就用google的gson包,不用就自己写字符串。

热点排行