读书人

servlet或struts的Action处置ajax请求

发布时间: 2012-09-04 14:19:30 作者: rapoo

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包,不用就自己写字符串。

读书人网 >Ajax

热点推荐