您现在的位置是:首页 > 电脑技术查询 > web开发

servlet或struts的Action处置ajax请求

编辑:chaxungu时间:2022-10-10 23:23:58分类:web开发

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