Summer Program Experience (ix)--Request Session Application

Source: Internet
Author: User

Request, Session, application

First, the basic knowledge

Can look at briefly: Request,session,application (http://blog.csdn.net/hzc543806053/article/details/7416007)

Points:

Scope: Request<session<application

Common methods: All can use SetAttribute (string name, Object o), getattribute (string name)

Second, get these three objects in Java

can see the following original text: Http://www.blogjava.net/xcp/archive/2009/10/23/struts2_actioncontext1.html?opt=admin

2.1 Three map after STRUTS2 package (using Actioncontext)

Import Java.util.Map; Import com.opensymphony.xwork2.Action; Import  == (Map) context.get ("request"== context.getapplication ();

2.2 access mode coupled to the Servlet API (non-IOC mode, using Servletactioncontext)

HttpServletRequest request == =  servletactioncontext.getrequest (). GetSession ();

Iii. The difference between actioncontext and Servletactioncontext

We know that STRUTS2 accepts client requests and returns the view results after processing in action. The struts2 container itself is not dependent on the Web container , and is not associated with a request, response (response) in the Servlet object , for the requested parameter, The parameters are encapsulated in the action by Paramerinterceptor, and the parameter values are set into action by calling the get and set methods. If you just get the parameters, you may sometimes not be able to meet the needs of development, sometimes we want to get the information in request or response, to set it up, processing.

3.1 Actioncontext

Is the context in which the action executes, the context of the action can be thought of as a container that encapsulates the request, session, application, and so on, which requests, sessions, Application is a map type, which encapsulates a key-value pair, so this shows that STRUTS2 does not work with the underlying Servlet API, so it encapsulates a lot of web-related objects so that the action can be decoupled from the web layer.

3.2 Servletactioncontext

It inherits Actioncontext, so Servletactioncontext can also get httpservetrequest, HttpServletResponse, and it also provides direct-to-servlet related objects access to the feature.

Summary: It is not difficult to see that there are still many common functions between the two, then we still choose according to their own needs, can use the Actioncontext object to meet the actioncontext as far as possible, to avoid allowing us to access the servlet directly Object . Also, do not call the Actioncontext method when the action is not instantiated, because the action instance is created before the Actioncontext instance, and some values in Actioncontext are not set and return null.

4. Application instances (statistics online)

4.1 Login Program in Loginaction.java

// Statistics Online number                MAP application = actioncontext.getcontext (). Getapplication ();                 = (Integer) application.get ("Count");                 if (null = = count) {                    count=1;                } Else {                    count+ +;                }                Application.put ("Count", count);

4.2 Unregistering programs in Exitaciton.java

MAP application = actioncontext.getcontext (). Getapplication ();         = (Integer) application.get ("Count");        Count--;        Application.put ("Count", count);

Summer Program Experience (ix)--Request Session Application

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.