简体   繁体   English

如何在J2ME中实现会话变量?

[英]How can I implement session variables in J2ME?

Is it possible to use session in J2me? 是否可以在J2me中使用会话? I am trying to develop a Blackberry application. 我正在尝试开发Blackberry应用程序。 I want to get some text from a class(page) and use in another class(page). 我想从一个类(页面)中获取一些文本,并在另一个类(页面)中使用。 May be something like the we do in app.net as: 可能像我们在app.net中所做的那样:

Class1
{
    session("myVariable") = Textbox.Text;
}

//a new class
Class2
{
    TextBox2.Text = session("myVariable").toString();
}

I will really appreciate for any type of help. 任何形式的帮助我都将非常感谢。

You can have a third class where you store such 'global' variables. 您可以拥有存储此类“全局”变量的第三类。 You can make these variables public and static; 您可以将这些变量设为公共和静态。 though this can lead to tightly coupled code. 尽管这会导致代码紧密耦合。

public class GlobalVariables
{
    public static string MyVariable = "empty";
}

Then you can do... 那你就可以...

GlobalVariables.MyVariable = Textbox.Text;

...and... ...和...

TextBox2.Text = GlobalVariables.MyVariable;

...anywhere in your code. ...代码中的任何地方。 While this would tend to be frowned upon in usual circumstances it can be useful when trying to write minimal, fast code to run on limited devices. 尽管在通常情况下这会让人皱眉,但在尝试编写最少的快速代码以在有限的设备上运行时可能很有用。

Another tip is to have a reset-method in GlobalVariables to reset all the static values back to their defaults in case the user wants to reset the app from within the app. 另一个技巧是在GlobalVariables中使用一种重置方法,以将所有静态值重置为默认值,以防用户想要从应用程序内部重置应用程序。 Also, if this is the only place you will store all your per-session variables you can add RMS save and load methods in here to keep it all in one place. 同样,如果这是您将存储所有每个会话变量的唯一位置,则可以在此处添加RMS保存和加载方法,以将其全部保存在一个位置。

Again, it's not the best way of doing things... but it is simple. 再说一次,这不是最好的做事方法……但是很简单。

When I first saw your question I thought it was going to be about HTTP sessions, but it looks like you're just after a way of storing state in your application. 当我第一次看到您的问题时,我以为这将是关于HTTP会话的,但是您似乎只是在寻找一种在应用程序中存储状态的方法。

There are a number of ways to do this. 有很多方法可以做到这一点。 Some things to consider: 要考虑的一些事情:

  • Do you need to persist the session data (or perhaps some of it) between runs of your application? 您是否需要在应用程序运行之间保留会话数据(或其中的一些数据)?
  • Is the set of session variables fixed, or can it vary? 会话变量集是否固定,或者可以变化?
  • Are all variables 'stringy' or are some numeric? 所有变量都是“字符串”还是某些数字?

A simple implementation might be an instance of Hashtable in a class with public static methods to get and set variables by calling the Hashtable put() and get() methods. 一个简单的实现可能是具有公共静态方法的类中Hashtable的实例,该方法通过调用Hashtable的put()和get()方法来获取和设置变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM