struts1.2获取action的方式是单例的,所有的action都被维护在一个hashMap里,当有请求到达时,先根据action的名称去hashMap里查找要请求的Action是否已经存在,如果存在,则直接返回hashMap里的action。如果不存在,则创建一个新的Action实例。
结论: struts1.x里的action是单例模式的,供多个请求共享。所以不是线程安全的。其数据都通过actionForm进行传递。因此,action实例里面不应该声明带有状态的实例变量
下面我们来分析一下源码:
请求到达ActionServlet时,首先会到达doGet()或doPost()方法,而ActionServlet转给了process()方法进行统一处理
1.public void doPost(HttpServletRequest request,
2. HttpServletResponse response)
3. throws IOException, ServletException {
4.
5. process(request, response);
6.
7.}
1.public void doPost(HttpServletRequest request,
2. HttpServletResponse response)
3. throws IOException, ServletException {
4.
5. process(request, response);
6.
7.}
而process()方法又会转给processor的process()方法进行处理
1.protected void process(HttpServletRequest request, HttpServletResponse response)
2. throws IOException, ServletException {
3.
4. ...
5. processor.process(request, response);
6.
7.}
1.protected void process(HttpServletRequest request, HttpServletResponse response)
2. throws