Java and flex Study Notes (19) -- session management in flex

Source: Internet
Author: User


Session is one of the nine built-in JSP objects, but it is not used in flex. I have read a book called Flex enterprise application development practice, which tells a little about the issue of using session to increase control permissions. I also tried to make a small demo.


One session class In Flex is the flexcontext class, which saves the session on the server.


Create a new Java flexsession. Java class, which contains various methods for storing, obtaining, and clearing sessions:

 

Package COM. ldfsoft. util; import flex. messaging. flexcontext; public class flexsession {/*** save session * @ Param name * @ Param value */Public void setsession (string name, string value) {flexcontext. getflexsession (). setattribute (name, value);}/*** get session */Public String getsession (string name) {string value = (string) flexcontext. getflexsession (). getattribute (name); return value;}/*** clear session * @ Param name */Public void removesession (string name) {flexcontext. getflexsession (). removeattribute (name );}}

 

Then configure this class in the flex configuration file.


Create a new mxml file login. mxml. The Code is as follows:


<? Xmlversion = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "> <FX: SCRIPT> <! [CDATA [import flash.net. navigatetoURL; import MX. controls. alert; import MX. events. flexevent; import MX. RPC. events. faultevent; import MX. RPC. events. resultevent; protected function login_clickhandler (Event: mouseevent): void {// todoauto-generated method stub var Username: String = username. text; var passworld: String = passworld. text; If (username! = "" & Passworld! = "") {// Save session sessionmanage. setsession ("Account", username);} else {alert. Show ("the user name and password cannot be blank! "," Prompt ") ;}} protected function sessionmanage_faulthandler (Event: faultevent): void {// todoauto-generated method stub alert. show (event. fault. message, "error");} protected function sessionmanage_resulthandler (Event: resultevent): void {// todoauto-generated method stub forwpage ();} private function forwpage (): void {var URL: URLRequest = new URLRequest ("http: // localhost: 9080/red5flex/SWF/welcome.html"); n Avigatetourl (URL, "_ Self") ;}]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visualelements (e.g ., services, value objects) Here --> <s: remoteobject id = "sessionmanage" Destination = "sessionmanage" endpoint = ".. /messagebroker/AMF "fault =" sessionmanage_faulthandler (event) "> <s: method name =" setsession "result =" sessionmanage_resulthandler (event) "/> </s: remoteobject> </FX: declarations> <s: panel x = "242" Y = "107" width = "549" Height = "367" Title = "Please log on to" fontsize = "16"> <s: label x = "99" Y = "106" text = "username:"/> <s: Label x = "100" Y = "175" text = "password: "/> <s: textinput x =" 179 "Y =" 106 "id =" username "/> <s: textinput x = "180" Y = "175" id = "passworld" displayaspassword = "true"/> <s: button x = "409" Y = "130" Height = "48" id = "login" label = "login" Click = "login_clickhandler (event)"/> </s: panel> </S: Application>

 

This file is a logon interface that stores user information in the session of the server.

 

Create a new mxml file welcome. mxml. The Code is as follows:


<? Xmlversion = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "applicationcomplete =" app_applicationcompletehandler (event) "> <FX: SCRIPT> <! [CDATA [import MX. controls. alert; import MX. events. flexevent; import MX. RPC. events. faultevent; import MX. RPC. events. resultevent; [Bindable] private var account: String = ""; protected function app_applicationcompletehandler (Event: flexevent): void {// todoauto-generated method stub // obtain session sessionmanage. getsession ("account");} protected function sessionmanage_resulthandler (Event: resultevent): void {// Note that account = event is used to obtain the session. result as string; message. TEXT = Account + ", welcome back... "} protected function sessionmanage_faulthandler (Event: faultevent): void {// todoauto-generated method stub alert. show ("failed to get session... "," prompt ");} protected function exist_clickhandler (Event: mouseevent): void {// todoauto-generated method stub // clear session sessionmanage. removesession ("account"); var URL: URLRequest = new u Rlrequest ("http: // localhost: 9080/red5flex/SWF/login.html"); navigatetoURL (URL, "_ Self");}]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visualelements (e.g ., services, value objects) Here --> <s: remoteobject id = "sessionmanage" Destination = "sessionmanage" endpoint = ".. /messagebroker/AMF "result =" sessionmanage_resulthandler (event) "fault =" sessionmanage_faulthandler (event) "/> </FX: declarations> <s: label x = "394" Y = "229" fontsize = "18" width = "229" Height = "31" id = "message"/> <s: button x = "443" Y = "283" label = "exit" id = "exist" fontsize = "16" Click = "exist_clickhandler (event)"/> </s: application>

 

When you jump to this page, the page will get account information from the server after initialization. If yes, it will be displayed on the page. Click "exit" to clear the session and jump to the logon page.


Run the login. mxml page as follows:




Input user information, click Login, and click here to go to The welcome.html page:


 


After you click the "exit" button, the page will jump to the homepage. When you run this page again, the user information will be null, or the session will not be obtained after you close your browser and open this page, this is a major feature of the session ~


By properly managing sessions, you can reduce the trouble of passing parameters and manage permissions. Even so, we do not advocate the use of sessions at work.


Original article, reproduced please indicate the source: http://www.dianfusoft.com/

 



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.