struts2是线程安全的在于每次都会实例化创建一个新的action,如果使用了HttpServletResponse也是每次请求action 创建实例的同时也会创建对应的response 还是会公用?还有用out(out.println())返回时的缓存区是会每个action创建呢还是共用一个
前几天遇到这样一个问题,做了一个接口,别人对我的action并发执行,最后那边的日志显示有两条返回结果是一致的,说是并发有问题,我自己打断点测试了一下,在out返回的地方打的断电,先用1号数据发送过去断点停了,然后2号数据在发送放开断点 查看控制台 返回的是2号数据 然后再放开1号断点,确实返回1号数据并没有变成2号,但是那个人说日志上是有两条相同的,然后就不知道该怎么样了,求大神指点,下面附录action中HttpServletResponse的引用和用printWriter的返回
public class BaseAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware, SessionAware, ServletContextAware, Serializable{
private static final long serialVersionUID = 1L;
protected ActionContext actionContext = ActionContext.getContext();
protected ServletContext servletContext;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Map<String,Object> session;
protected String path;
protected String msg;
public String getPath() {
if(path==null||path.equals("")){
return request.getContextPath();
}
return path;
}
public void setPath(String path) {
this.path = path;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
public void setSession(Map<String, Object> session) {
this.session=session;
}
public void setServletContext(ServletContext context) {
this.servletContext=context;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
public void sendJson(String json) throws IOException{
PrintWriter out = response.getWriter();
out.println(json);
out.flush();
out.close();
}
------解决方案--------------------
Spring创建的Action默认是单例