[Java] [web] Use spring to get request and session anywhere

Source: Internet
Author: User

Use spring to GET request and session anytime, anywhere

First, the preparatory work:

Add to Web. xml

<listener>            <listener-class>                Org.springframework.web.context.request.RequestContextListener        </listener-class>    </listener>


Second, usage:
1, method One: through the code implementation

HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ;



2, method Two: through annotations to achieve:

@Autowiredprivate  httpservletrequest request;



Third, the background knowledge about Requestcontextlistener:binds an HTTP request object to a thread that serves the request based on Localthread. This allows the bean with the request and session scope to be interviewed in the subsequent call chain.

Request Scope
<bean id= "loginaction" class= "com.foo.LoginAction" scope= "Request"/>


For each HTTP request. The spring container creates a completely new instance of the Loginaction bean based on the loginaction bean definition, and the loginaction bean instance is valid only within the current HTTP request. It is therefore possible to change the internal state of the built instance in confidence. In other requests, instances created from the loginaction bean definition will not see these state changes specific to a request.

When the processing request ends, the bean instance of the request scope is destroyed.

Session Scope
<bean id= "userpreferences" class= "Com.foo.UserPreferences" scope= "session"/>
for an HTTP Session. The spring container creates a completely new instance of the Userpreferences bean based on the userpreferences bean definition, and the userpreferences Bean is valid only within the current HTTP session. As with the request scope, you can change the internal state of the created instance based on the need to be assured, and other HTTP sessions that are created from Userpreferences will not see the state changes that are specific to an HTTP session. When the HTTP session is finally discarded, the beans inside the HTTP session scope are discarded.



Global Session Scope

<bean id= "userpreferences" class= "com.foo.UserPreferences" scope= "globalsession"/>

The global session scope is similar to the standard HTTP session scope, except that it makes sense only in Portlet-based Web applications. The Portlet specification defines the concept of a global session. It is shared by a variety of portlets that make up a portlet Web application.

The bean defined in the global session scope is limited to the lifetime of the world Portlet session.

Note that if you are writing a standard servlet-based Web application and you have defined one or more beans with the global session scope, the system uses the standard HTTP session scope and does not cause any errors
Second, why the need for additional configuration Requestcontextfilter
There may be a doubt that the Web container has been integrated with the spring container through Contextloaderlistener (or Contextloaderservlet). Why is there an additional requestcontextlistener to support the other 3 scopes of the bean, because Contextloaderlistener implements the Servletcontextlistener listener interface, Servletcontextlistener is only responsible for monitoring the startup and shutdown events of the Web container.

The Requestcontextfilter implements the Servletrequestlistener listener interface, which listens for HTTP request events and notifies the listener webserver every request received. The Requestcontextfilter,spring container is more closely combined with the Web container by configuring it.


Three, scope dependence problem

Suppose you inject a web-related scope bean into a singleton or prototype bean, in such a case. Requires Spring AOP

<bean name="Car" class="Com.demo.Car" scope="Request">

<aop:scoped-proxy/>

</bean>

<bean id="Boss" class="Com.demo.Boss" >

<properrty name="car " ref="car "/>

</bean>



[Java] [web] Use spring to get request and session anywhere

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.