The difference between Actioncontext and servletactioncontext---to be continued

Source: Internet
Author: User

  

    

 //
public class Baseaction extends actionsupport{
public static HttpServletRequest Getrequest () {
return Servletactioncontext.getrequest ();
}
public static HttpServletResponse GetResponse () {
return Servletactioncontext.getresponse ();
}
public static HttpSession getsession () {
Return Servletactioncontext.getrequest (). GetSession ();
}
public static ServletContext Getapplication () {
return Servletactioncontext.getservletcontext ();
}
}
//

//A blog to see the ideas
Map request= (map) Actioncontext.getcontext (). Get ("request");
Map request= (map) Actioncontext.getcontext (). Get ("session");
Actioncontext (). GetContext (). Get ("request");//
Actioncontext (). GetContext (). GetSession ()//Get the session, and then use put to save the corresponding value, as long as the session is valid state, this value is always available
Actioncontext (). GetContext (). GetSession (). Put ("X", X);//object is placed in map by default
Actioncontext (). GetContext (). Getvaluestack (). push (x);//In Struts2, a request is pressed into the action object itself before it finally arrives at the action method Valuestack , you can press an object directly into the Valuestack.
//About the usage of actioncontext.getcontext ()
Actioncontext Context=actioncontext.getcontext ();
Map Request=context.get ("request");
Map session=context.getsession ();
Map application=context.getapplication ();
Request.put ("Greeting", "Welcome to Eric Tsang Space");//Save the information in the request
Session.put ("user", user);//Save user object in session
Application ("Counter", count);
Use directly from the JSP page
<body>
</body>
Of course, you can use it directly.
Actioncontext.getcontext (). Put ("greeting", "Welcome to visit my website");
${requestscope.greeting} or <%=request.getattribute ("greeting")%>
//Baidu Encyclopedia: What's the difference between Actioncontext and ServletContext
Servletactioncontext and Actioncontext have some repetitive functions, in our action,
How to make a choice? The principle we follow is: if Actioncontext can achieve our function,
It's best not to use servletactioncontext and let our action try not to access the Servlet object directly.
Note: There is one thing to note when using Actioncontext:
Do not use Actioncontext.getcontext () in the action's constructor,
Because some values in Actioncontext may not be set at this time, the value obtained by actioncontext may be null;
Similarly, httpservletrequest req = Servletactioncontext.getrequest () is not placed in the constructor,
Do not assign a value directly to the Req as a class variable. As for the reason, I think it is because of the static ThreadLocal Actioncontext = new Actioncontextthreadlocal () mentioned earlier,
From here we can see that Actioncontext is thread-safe, and Servletactioncontext inherits from Actioncontext, so Servletactioncontext is also thread safe,
Thread safety requires that each thread be independent, so the creation of the Req also requires independence, so servletactioncontext.getrequest () is not placed in the constructor,
Also do not put directly in the class, but should be placed in each specific method body (Eg:login (), Queryall (), insert (), etc.), so as to ensure that each time the object is created independently of the establishment
A req.
//

Http://www.cnblogs.com/ningvsban/p/3740717.html

Http://www.cnblogs.com/xwdreamer/archive/2010/09/26/2297083.html

The difference between Actioncontext and servletactioncontext---to be continued

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.