How to create an object only if not exist?

how to create an object only if not exist? is it possible?
if not what can I do?
Thanks.

Well, the jsp:useBean tag is a great way to do things.
But another way is:
<%  
      MyObjectType myObject = null;
      if(session.getAttribute("myObject"))==null) {
         myObject = ... ; //initiate your object
         session.setAttribute("myObject", myObject);
      myObject = (MyObjectType) session.getAttribute("myObject");
%>As you can see, this is more messy, but you can do it.

Similar Messages

  • How to create an object ONLY IF a condition is true

    I hope this is the correct place to post this....*takes deep breath*:
    I am writing a fairly basic program in NetBeans. The following example will simplify what I wish to convey, and ask for advice about:
    Imagine a program consisting of several classes. The constructor of one of these classes accepts three strings, entered by a user, as arguments. The idea is that the constructor of this class will then:
    -> Check that these strings all contain integers (using Integer.parseInt).
    -> If all three of these strings contain integers, check that all three of these do not add up to over 100.
    If both of these tests pass, then the object is created. What I want to know is how can I say "If either or both of these tests fail, DO NOT CREATE A NEW OBJECT ".
    If either or both of these tests are not passed, I want to display an error message, but then recover gracefully. I DO NOT want to simply do:
    exit(0);
    and kill the whole program. Nor do I want to shove the error checking in to the 'main()' class (where it already is), because, if someone else uses that other class, without my 'main()' class, they may pass it garbage. I feel it would be most sensible if the above tests were performed WITHIN THE ACTUAL CLASSES CONSTRUCTOR. But I want this done in a way where the constructor can respond with something like "Please use three integers", or "Object could not be created, please try again". Then the 'main()' class, having recieved, say, a boolean 'false' as the return type, can prompt the user to re-enter the strings (BTW, a return of 'true' to the 'main()' class would indicate that the object had been properly created, so the 'main()' class could carry on).
    Does this make sense???!!
    Thank you very much in advance,
    ./b

    Yeah, you're probably right. From what the OP said I
    imagined that errors in the constructor would be quite
    common, i.e. not exceptional at all, thus I
    didn't use them. If a caller passes an invalid parameter, I'd throw IllegalArgumentException, since passing an invalid parameter is a logic bug in the code. If the constructor connects to a DB or reads a file or something, I'd declare it to throw IOException or SQLException or some custom wrapper.
    If exceptions were thrown in the
    factory method I would also add another static method
    something like:
    public static boolean
    areTheseConstructorParametersValid( String a , String
    b , String c ) {...}to give the client the option of avoiding exception
    handling if they wanted. I wouldn't use this method
    name though...I don't really see any benefit to that. It goes back to the code sample I posted. If a client uses that, then he has the same kind of test as checking for null, it's just that it occurs before calling the factory method. And he still has to catch the exception anyway (if it's checked), so it doesn't really buy you anything.
    It's true that if the client is getting arbitrary input from, say, a user or a file, then he should validate that before calling the method. Not doing so is a logic bug that should produce IllegalArgumentException. And if the validation rules are complex, it might be a nice convenience function for the class to provide to clients, but I wouldn't use it as a replacement for exceptions.
    Just my $0.02.

  • How to create an object for this Question...

    i want to create an object to Factorial.java, but the class is in String object... how can i do that
    please help me with code how to create an object
    String str = Factorial.java;
    i tried to do like this
    str s1 = new str();
    this way is not working...
    thank u

    yes sir am trying to instantiate and retrieve the metadata, am a student am asked to do Program Analyzer, while fullfilling the application requirements i came across this situation, like i have to find total number of public methods in any source file that is entered as input.
    Am taking the input in String variable, i can have the details of the source class only of i instantiate and create an Object to that....
    Please suggest me some way to do that... JAVA is so interesting am working on the project since 2 weeks am done with all other classes but struck here finding the total number of methods...
    Thank you

  • How to create an object of type 99

    Hi guys,
    Am struck creating a HR object with subty = 99.
    I was trying to BDC  PP01 transaction.
    But I dont see the object type 99.
    They call it as FI hierarchy node...
    Am a HR Tech consultant..
    Could not understand what that means...
    Please help me how to create an object in HR with subty 99
    Thanks in advance...

    Class.forName does not create an object. It returns a reference to the Class object that describes the metadata for the class in question--what methods and fields it has, etc.
    To create an object--regardless of whether it's your class or some other class--you could call newInstance on the Class object returned from Class.forName, BUT only if that class has a no-arg constructor that you want to call.
    Class<MyClass> clazz = Class.forName("com.mycompany.MyClass");
    MyClass mine = clazz.newInstance();If you want to use a constructor that takes parameters, you'll have to use java.lang.reflect.Constructor.
    Google for java reflection tutorial for more details.
    BUT reflection is often abused, and often employe when not needed. Why is it that you think you need this?

  • How to create an object of our own class by using Class.forName()??

    how to create an object of our own class by using Class.forName()??
    plzz anser my qustion soon..

    Class.forName does not create an object. It returns a reference to the Class object that describes the metadata for the class in question--what methods and fields it has, etc.
    To create an object--regardless of whether it's your class or some other class--you could call newInstance on the Class object returned from Class.forName, BUT only if that class has a no-arg constructor that you want to call.
    Class<MyClass> clazz = Class.forName("com.mycompany.MyClass");
    MyClass mine = clazz.newInstance();If you want to use a constructor that takes parameters, you'll have to use java.lang.reflect.Constructor.
    Google for java reflection tutorial for more details.
    BUT reflection is often abused, and often employe when not needed. Why is it that you think you need this?

  • How to create authorisation object for save button please help in abap

    how to create authorisation object for save button please help in abap

    Hi
    In general different users will be given different authorizations based on their role in the orgn.
    We create ROLES and assign the Authorization and TCODES for that role, so only that user can have access to those T Codes.
    USe SUIM and SU21 T codes for this.
    Much of the data in an R/3 system has to be protected so that unauthorized users cannot access it. Therefore the appropriate authorization is required before a user can carry out certain actions in the system. When you log on to the R/3 system, the system checks in the user master record to see which transactions you are authorized to use. An authorization check is implemented for every sensitive transaction.
    If you wish to protect a transaction that you have programmed yourself, then you must implement an authorization check.
    This means you have to allocate an authorization object in the definition of the transaction.
    For example:
    program an AUTHORITY-CHECK.
    AUTHORITY-CHECK OBJECT <authorization object>
    ID <authority field 1> FIELD <field value 1>.
    ID <authority field 2> FIELD <field value 2>.
    ID <authority-field n> FIELD <field value n>.
    The OBJECT parameter specifies the authorization object.
    The ID parameter specifies an authorization field (in the authorization object).
    The FIELD parameter specifies a value for the authorization field.
    The authorization object and its fields have to be suitable for the transaction. In most cases you will be able to use the existing authorization objects to protect your data. But new developments may require that you define new authorization objects and fields.
    http://help.sap.com/saphelp_nw04s/helpdata/en/52/67167f439b11d1896f0000e8322d00/content.htm
    To ensure that a user has the appropriate authorizations when he or she performs an action, users are subject to authorization checks.
    Authorization : An authorization enables you to perform a particular activity in the SAP System, based on a set of authorization object field values.
    You program the authorization check using the ABAP statement AUTHORITY-CHECK.
    AUTHORITY-CHECK OBJECT 'S_TRVL_BKS'
    ID 'ACTVT' FIELD '02'
    ID 'CUSTTYPE' FIELD 'B'.
    IF SY-SUBRC <> 0.
    MESSAGE E...
    ENDIF.
    'S_TRVL_BKS' is a auth. object
    ID 'ACTVT' FIELD '02' in place 2 you can put 1,2, 3 for change create or display.
    The AUTHORITY-CHECK checks whether a user has the appropriate authorization to execute a particular activity.
    This Authorization concept is somewhat linked with BASIS people.
    As a developer you may not have access to access to SU21 Transaction where you have to define, authorizations, Objects and for nthat object you assign fields and values. Another Tcode is PFCG where you can assign these authrization objects and TCodes for a  profile and that profile in turn attached to a particular user.
    Take the help of the basis Guy and create and use.
    Regards
    ANJI

  • How to create a object for IPageItemControlData?

    how to create a object for IPageItemControlData?

    From the header comment you'll see that IPageItemControlData is an Interface of PageItemWidget. That must've been an InDesign 1.0 or 1.5 feature predating kPageItemBoss, because except for the same comment even in InDesign 2.0 SDK the only further reference is a copy in the comment of its sibbling IFrameControlData which was removed with InDesign CS.
    The IID and kPageItemControlDataImpl are probably only around because deep under the hood there must be some kind of conversion provider waiting to do its job on a very old document.
    I guess the closest in today's functionality will be IHierarchy.
    Dirk

  • How to create Activex object for my Visual C++ object

    Hi,
    I am working on development on Acrobat 9.0 SDK. I am facing problem is that I can compiled Visual C++ source code to an api. I can test functions on this api on Acrobat 9.0 windows, but I could not test it on IE web browser, because I don't know how to create Activex object from my Visual C++ source code or its api.
    I read Acrobat 9 SDK document and found under C:\Acrobat 9 SDK\Version 1\PluginSupport\Tools\Visual Studio App Wizard has two files: Acro9PIWizInstaller.msi and setup.exe. I am not very sure those file are the key to help me to create Activex objects for my api.  Are they the one I need to create Activex object? If not, could you please advise me how to create Activex object for my api or C++ codes.
    Thanks a lot for any of your comments or advices.
    Thai

    Hi lrosenth,
    Thanks a lot for your information.
    My question is, on Javascript how do I call a function from .api. Below is my very simple test.
    I got a function on BasicPlugin.cpp under C:\Acrobat 9 SDK\Version 1\PluginSupport\Samples\BasicPlugin. See below.
    I added BasicPlugin.api on C:\Program Files\Adobe\Acrobat 9.0\Acrobat\plug_ins
         ACCB1 int ACCB2 MyPluginCommand() {
              int num = 5;
              return num;
    On my HTML file, I create an <Object> below:
        <OBJECT id="acrobatapp"
                classid="clsid:85DE1C45-2C66-101B-B02E-04021C009402">
        </OBJECT>
        <OBJECT id="acrobatpdf"
                classid="clsid:CA8A9780-280D-11CF-A24D-444553540000"   >
            <Param name="SRC" value="http://www.adobe.com/devnet/acrobat/pdfs/acrobat_digital_signature_appearances_v9.pdf">
       </OBJECT>
    On my Javascript. I call function MyPluginCommand() as below, but it is not working. How do I call function MyPluginCommand() on Javascript?
         var acrobatapp= document.getElementById("acrobatapp");
         var num= acrobatapp.MyPluginCommand();
         alert(num);
    Thanks for your support,
    Thai

  • How to create an object alive all the time on a server side

    Hi,
    I work on an Intranet with J2EE technologies. I need an object to do several tasks in background on the server side. Like start scheduled processes on a database and files, stocked on the server. What is the way to do this. I thought to use the JNDI by declaring the object in the Deployement Descriptor, and to manage a timer within.
    I don't know if this works.
    Anyway, my question is how to create an object on the server that is ALIVE ALL THE TIME and visible by the other objects running in the same VM.
    Thank's.

    This may not be the correct design, but I had success doing the following:
    1. Create an Stateless EJB that does a task, i.e. update a database table.
    2. Create a subclass of java.util.TimerTask to call the EJB methods.
    3. Kick off the TimerTask from a servlet.init method.
    Seems to work pretty well to do scheduled tasks, such as refreshing lookup tables. I would imagine the same could be done with an MBean, but I haven't had time to fully understand JMX yet. (It's on the list).
    As for event driven procedures, that can be done through a servlet interface.
    You may also wish to review a scheduler here: http://www.part.net/quartz.html. Note, I have NOT even looked at it yet, so I have no clue if it's any good. But, it is on my list of tools to evaluate (that's a long list) someday when my head's above water. Have a look, and caveat emptor.
    Hope this helps,
    Perry Tew

  • How to create an object of a class?

    plz read the question carefully ..
    how to create the object of a class by giving the class name as an String
    suppose i have a java file name:" java1.java ".
    so now how to create an object of this class "java1"by passing a string "java1"..

    kajbj wrote:
    rajeev-usendi wrote:
    thanks but still i have problem..
    i have created the object but now i m unable to do anything with that created object..
    i have coded like this..
    Object o= Class.forName("java1").new Instance();
    after this i m unable to do anything with the object 'o'..So why did you create the instance? You can also get all methods using reflection, but that is probably not what you want to do. You should instead let the class implement an interface and cast the created object into that interface ans call the methods that you want to call.
    KajI agree with Kaj. If you need to use a class that's unavailable at compile time, you should create an appropriate interface by which to refer to the class and then you create instances reflectively and access them normally via their interface (which is called reflective instantiation with interface access)

  • How to create an object class programmatically?

    Guys,
    I need your help. If possible with a simple example of how to do it.
    I need to create an aplication that read an specification of a repository type from a file and then creates it inside my BEA Repository. The problem are few:
    1 - How to Create an Object Class programmaticaly?
    I looked at documentation of ObjectClassOps but havent figured out how to do it.
    2 - How to Create a Property Definition programmaticaly?

    Sorry, ITypeManager only exists in 9.2. If you are working on 8.1, then the previous example becomes:
    RepositoryManager repositoryMgr = RepositoryManagerFactory.create();
    //connect to the repository
    repositoryMgr.connect();
    //get ObjectClassOps
    ObjectClassOps ocops = repositoryMgr.getObjectClassOps();
    ID repId = new ID();
    repId.setRepositoryName(repositoryName);
    PropertyDefinition[] pds = new PropertyDefinition[1];
    pds[0] = new PropertyDefinition(repId, "stringProp", null, Property.STRING, false, false, false, false, false, null);
    ObjectClass oc = new ObjectClass(repId, "myType", null, pds);
    oc = ocops.createObjectClass(oc);

  • How to create an object within the same class???

    hi im just a newbie
    i v been always creating an object from the main class..
    but how to create an object inside the same class??
    i got main and students class
    the main got an array
    Students[] stu = new Students[]
    and i got
    stu[i] = new Students(id,name);
    i++;
    but i wanna do these things inside the Students class..
    i tried ..but i got errors.....
    how to do this
    .

    javaexpert, :)
    I really have no idea what you are trying to do since you say you've always been creating an object from the main class, yet you always want to create an object inside the same class.
    I'll assume that you have an object in the main class that you are trying to access from the Students class.
    If you are trying to access objects that are contained within the main class FROM the Students class, then know that there are two ways of doing so, one being static, and the other dynamic (look up definitions if unclear):
    1.) make the objects in the main class both static and public and access the the objects using a convention similiar to: Main.object; It's important to note that Main is the name of your main class, and object is a static object. There are better ways of doing this by using gettter/setter methods, but I'll omit that tutorial.
    2.) Create a new instance of the main class and access the objects using a similiar fashion: Main myInstance = new Main(); myInstance.myObject;
    You should really use getter and setter methods but I'll omit the code. In terms of which approach is better, step one is by far.
    I don't mean to be condecending, but you should really brush up on your programming skills before posting to this forum. This is a fundamenetal concept that you will encounter time and time again.

  • How to create an object of inner class

    hi i don't know how to create an object of an inner class..
    i got something like
    class Abc{
    private class Abcd {
    like this and i want to create an object of Abcd so that i can use some of method
    there..
    i think i should create the outter class object first right? but not sure
    the syntax.. i tried something like
    Abc.Abcd justTry = new Abc.Abcd()
    something like this..but not work..
    help me plz. ...

    If the nested class (that's not technically an inner
    class you have there) is not static, then you can't
    refer to it with OuterClassName.InnerClassName any
    more than you can refer to any other member--method
    or variable--with ClassName.staticMember.
    You need an instance.
    It's been a while since I've created an instance of a
    non-static nested class from outside that clsas, but
    I think it's something like this: Outer outer = new Outer();
    outer.Inner inner = new outer.Inner();
    Actually, I think it is this:
      Outer outer = new Outer();
      outer.Inner inner = outer.new Inner();Can't test it now though, gotta go to Taco John's for TACO TUESDAY!!!!
    Gotta love them crunchy shell bean tacos!!!

  • How to create Anchored Object (CS2)

    Hi,
    Indesign CS2(PC version)
    Javascript
    Any one help me, How to create Anchored object within TextFrame and inside anchor object i want to paste one image.
    by
    Sudar

    Hi Hemi,
    Sure--tell the insertion point to create the object. For example, to add a text frame:
    //Given a reference to an insertion point "myInsertionPoint"...
    var myFrame = myInsertionPoint.textFrames.add();
    Now you have a text frame anchored/inline in the story. Next, recompose the story containing the frame:
    myInsertionPoint.parentStory.recompose();
    ...and resize the frame.
    //Given a frame width and height "myFrameWidth" and "myFrameHeight"...
    var myX1 = myFrame.geometricBounds[1];
    var myY1 = myFrame.geometricBounds[0];
    var myX2 = myX1 + myFrameWidth;
    var myY2 = myY1 + myFrameHeight;
    myFrame.geometricBounds = [myY1, myX1, myY2, myX2];
    At this point, you can adjust the frame's anchored object settings (your settings will probably vary, but this should give you the idea).
    var myAnchoredProperties = myFrame.anchoredObjectSettings;
    myAnchoredProperties.anchorPoint = AnchorPoint.topLeftAnchor;
    myAnchoredProperties.anchoredPosition = AnchorPosition.anchored;
    //NOTE inconsistent "o" in "offset".
    myAnchoredProperties.anchorXoffset = 0;
    myAnchoredProperties.anchorYoffset = -13;
    Hope this helps!
    Thanks,
    Ole

  • How to create a audio only conference at MCU 5320 .. ?

    How to create a audio only conference at MCU 5320 .. ?

    Hi Hermanus,
    Please, post this question to Telepresence support community as this community is for Cisco WebEx Meetings Server and Cisco Unified MeetingPlace products and is not being observed by TP experts. Hence, you will not be able to get an answer to your questions.
    I hope this helps.
    -Dejan

Maybe you are looking for