How do i create a single instance of a class inside a servlet ?

how do i create a single instance of a class inside a servlet ?
public void doGet(HttpServletRequest request,HttpServletResponseresponse) throws ServletException, IOException {
// call a class here. this class should create only single instance, //though we know servlet are multithreaded. if, at any time 10 user comes //and access this servlet still there would one and only one instance of //that class.
How do i make my class ? class is supposed to write some info to text file.

i have a class MyClass. this class creates a thread.
i just want to run MyClass only once in my servlet. i am afriad, if there are 10 users access this servlet ,then 10 Myclass instance wouldbe created. i just want to avoid this. i want to make only one instance of this class.
How do i do ?
they have this code in the link you provided.
public class SingletonObject
  private SingletonObject()
    // no code req'd
  public static SingletonObject getSingletonObject()
    if (ref == null)
        // it's ok, we can call this constructor
        ref = new SingletonObject();          
    return ref;
  public Object clone()
     throws CloneNotSupportedException
    throw new CloneNotSupportedException();
    // that'll teach 'em
  private static SingletonObject ref;
}i see, they are using clone !, i dont need this. do i ? shouldi delete that method ?
where do i put my thread's run method in this snippet ?

Similar Messages

  • How do I create an genericized instance of this class?

    Hi, I'm having trouble creating a proper generic instance of this class that I wrote. The class type is cyclic dependant on itself.
    public class MyClass<T extends MyClass<T>>
    I can create an instance like this:
    MyClass<?> class = new MyClass();But I would be mixing raw types and generic types.
    Is there anyway to create a proper generic instance of MyClass?
    Thanks a lot
    -Cuppo

    Ah thanks georgemc,
    your solution will certainly work.
    If i may ask your opinion of something...
    MyClass is designed such that the type parameter should always be the same as the class itself. It's kinda a hack to get the subclass type from the superclass.
    so a hierarchy looks something like this:
    class MyClass<T extends MyClass<T> >
    class SubClass<T extends SubClass<T>> extends MyClass<T>
    class FinalSubClass extends SubClass<FinalSubClass>So ideally i would want something like:
    MyClass m = new MyClass<MyClass>();
    (which is not possible)
    is there a way to get around this?
    -Cuppo

  • How can i create a new instance on unix

    Hi,
    How can i create a new instance(instance only) on UNIX and how can i mount my database with this new instance.

    udayjampani wrote:
    Hi,
    How can i create a new instance(instance only) on UNIX and how can i mount my database with this new instance.1) Define Instance in your way!!?
    If you simply create a pfile and startup nomount with it, then -in theory- you have an instance running ( this method is used e.g. for restoring a rman backup )
    2) You can't. Database files are bound to a certain instance. What you can do is recreate an instance with e.g. a different name, using the scripts generated by a 'alter database backup controlfile to trace;' command
    Cheers
    FJFranken

  • How can I create a single order from multiple quotations?

    How can I create a single order from multiple quotations that I have created by the transaction VA21 ?
    Thanks in advance for the answers.

    hi
    Go to transaction: /nva01
    Enter order type : ZOR
    Sale org :xxxx
    Dist.channel:xx
    Division :xx
    Press enter
    Click on “Sale document” and select Create with reference
    Then enter 1st quotation number & click on “COPY” or “Selection list”. Then click on “Copy “.Then all line items which belong to quoation1 copy to order.
    Then,
    Click on “Sale document” and select Create with reference
    Then enter 2nd quotation number & click on “COPY” or “Selection list”. Then click on “Copy “.Then all line items which belong to quoation2 copy to order.
    Then,
    Click on “Sale document” and select Create with reference
    Then enter 3rd quotation number & click on “COPY” or “Selection list”. Then click on “Copy “.Then all line items which belong to quoation3 copy to order.
    Now save the sale document.
    Kindly give reward points
    Edited by: WISH on Mar 19, 2008 2:25 PM

  • How do I create a single PDF Portfolio from an Outlook 2011 email with multiple non-pdf attachments?

    How do I create a single PDF Portfolio from an Outlook 2011 email with multiple non-pdf attachments?
    Email has 3 attachments--some are not pdf. I'd like all three converted into pdf files along with the email itself, and all appear in the email's pdf portfolio.

    I would also like an answer to this question. 
    I am trying to convert an Outlook email to a PDF, then all attachment are appended to the PDF as pages instead of attachments. 

  • CREATING A SINGLE INSTANCE PHYSICAL STANDBY FOR A RAC PRIMARY

    Hi
    Creating a single instance physical standby database for a RAC Primary.
    Getting this error.
    sql statement: alter database mount standby database
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of Duplicate Db command at 01/17/2008 23:05:38
    RMAN-03015: error occurred in stored script Memory Script
    RMAN-03009: failure of sql command on clone_default channel at 01/17/2008 23:05:38
    RMAN-11003: failure during parse/execution of SQL statement: alter database mount standby database
    ORA-01103: database name 'PROD' in control file is not 'DPROD'
    Any help on this.
    Regards
    Satish

    The problem here is probably with your standby init.ora file.
    When you create a standby database, the db_name parameter must NOT change. It has to match the primary database. So in your case, db_name ='PROD' and your db_unique_name='DPROD'...
    -peter

  • How to change an ASM single-instance  into a clustered ASM ?

    Hi,
    I have a node#1 with Oracle10gR2 installed (standard installation, not RAC).
    Everything is in the ORACLE_HOME.
    A unique single-instance database DB1 uses ASM#1 to access a diskgroup on a SAN.
    I'd like to install Clusterware in a separate CRS_HOME (within the oracle account), and protect ASM#1 and the database DB1 using an application VIP.
    The things is: I want to create another single-instance database DB2 on another node#2, with another ASM#2 instance.
    I need a clustered ASM (with Clusterware) installation to do that because ASM#1 and ASM#2 need to access the same diskgroup.
    I don't want to lose the existing data & configuration of the ASM#1 instance.
    What modifications I need to do in the current ASM#1 instance on node#1 to make it Clusterware-aware ?
    (Again, it's not a RAC installation...)
    Thanks

    Hi,
    I finally found this Metalink Note 452758.1, which describe the procedure....
    Any feedback about this ?

  • How do I create a main instance?

    Hello,
    when I want to create an application component in the process scenario edtior I can choose a "main instance". However how do I create a main instance? I suppose that this has to be done in SLD but I found nothing there.
    When can a main instance and a product be different? I do not yet fully understand the concept behind it as both consist of several product component versions that can be installed on a technical system, right?
    Any explanation is highly appreciated. Thank you.

    Hi!
    I hope this makes things clearer:
    "A main instance is a group of technically interdependent software component versions, which are all installed and run on one server. A main instance represents the smallest non-divisible unit within a system landscape. An application component in a Process Integration scenario represents a system at configuration time and at runtime. Therefore, use main instances by default. This is because main instances represent the software component versions installed on a system. Only if a product comprises exactly one main instance may you also use a product version."
    Regards,
    Volker

  • How do I create a single image using PSE12 from multiple originals where each original is still separate?

    How do I create a single image using PSE12 from multiple originals in which the originals are still separate (i.e. side by side or arranged in a square)? I could do this in PSE2, but can't find how to get PSE12 to do it.
    Grecophile55

    In PSE2 there was a single photo-merge. I browsed all my images and I could then click and drag each to its new position before saving. There doesn’t seem to be a similar facility in PSE12.

  • How can I store a single instance of a hashtable

    How can I store a single instance of a hashtable in a predefined application object that way I can use it by all sessions? I've tried storing a instance of hashtable in the session but it only allows for one user and it doesn't accomplish my goal in the web application. Could show me how i could store a single instance of a hashtable in a predefined application object? Or at least a link which demonstrates it
    thanks

    assuming ctx is an instance of javax.servlet.ServletContext (which can be retrieved from the ServletConfig).
    ctx.setAttribute("yourAttrName", new Hashtable());

  • Create a new instance of a class without starting new windows.

    I've a class with basic SWING functions that displays an interface. Within this class is a method which updates the status bar on the interface. I want to be able to reference this method from other classes in order to update the status bar. However, in order to do this, I seem to have to create a new instance of the class which contains the SWING code, and therefore it creates a new window.
    Can somebody give me an example, showing how I might update a component on the interface without a new window being created.
    Many thanks in advance for any help offererd.

    I've a class with basic SWING functions that displays
    an interface. Within this class is a method which
    updates the status bar on the interface. I want to be
    able to reference this method from other classes in
    order to update the status bar. However, in order to
    do this, I seem to have to create a new instance of
    the class which contains the SWING code, and
    therefore it creates a new window.
    Can somebody give me an example, showing how I might
    update a component on the interface without a new
    window being created.
    Many thanks in advance for any help offererd.It sounds like you have a class that extends JFrame or such like and your code must be going
               Blah  test = new Blah();
                test.showStatus("text");Whereas all you need is a reference to that Classs.
    So in your class with basic SWING functions that displays an interface.
    You Might have something like this
              // The Label for displaying Status messages on
              JLabel statusLabel = new JLabel();
              this.add( statusLabel , BorderLayout.SOUTH );What you want to do is provide other Classes a 'method' for changing your Status Label, so in that class you might do something like:
          Allow Setting of Text in A JLabel From various Threads.
         And of course other classes......
         The JLabel statusLabel is a member of this class.
         ie defined in this class.
        @param inText    the new Text to display
       public void setStatusText( String inText )
                    final String x = inText;
                    SwingUtilities.invokeLater( new Runnable()
                              public void run()
                                       statusLabel.setText( x );
      }You still need a reference to your first class in your second class though.
    So you might have something like this:
            private firstClass firstClassReference;        // Store Reference
            public secondClass(  firstClass  oneWindow )
                          // create whatever.........
                         this.firstClassReference = oneWindow;
    // blah blah.
          private someMethod()
                            firstClassReference.setStatusText( "Hello from Second Class");
        }Hope that gives you some ideas

  • Managing Multiple threads accessing  a single instance of a class

    Hi,
    i have to redesign a class, say X, in such a way that i manage multiple threads accessing a single instance of the class, we cannot create multiple instances of X. The class looks like this:
    Class X{
    boolean isACalled=false;
    boolean isInitCalled=false;
    boolean isBCalled=false;
    A(){
    isACalled=true;
    Init(){
    if(!isACalled)
    A();
    B();
    C();
    isInitCalled=true;
    B(){
    if(!isACalled)
    A();
    isBCalled=true;
    C(){
    if(!isACalled)
    A();
    if(!isBCalled)
    B();
    }//end of class
    Init is the method that would be invoked on the single instance of this class.
    Now i cannot keep the flags as instance variables coz different threads would have differrent status of these flags at the same time, hence i can make them local, but if i make them local to one method, the others won't be able to check their status, so the only solution i can think of is to place all the flags in a hashtable local to method INIT AND INITIALIZE ALL OF them to false, as init would call other methods, it would pass the hashtable reference as an additional parameter, the methods would set the flags in the hashtable and it would be reflectecd in the original hashtable, and so all the methods can have access to the hashtable of flags and can perform their respective checks and setting of flags.
    This all would be local to one thread, so there's no question of flags of one thread mixin with the flags of some other thread.
    My question is :
    Is this the best way, would this work?
    In java, everything is pass by value, but if i pass the hashtable reference, would the changes made inside the called method to the hashtable key-value would be visible in the original hashtable declared inside the calling method of which the hashtable is local variable?

    In Java object variables are passed "by copy of reference", and primitive variables "by value".
    The solution with HashMap/Hashtable you suggest is ok, but I think you should read about ThreadLocal class:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ThreadLocal.html

  • How do I create a second instance of oc4j in a standalone enviroment

    I am using OC4J standalone to deploy a sparql endpoint using jena, joseki, and the oracle jena adaptor. OC4J is running on the same server as my DEV and QAR databases. The current instance of OC4J is using a data source called "OracleSemDS" to connect to my DEV database. We are now ready to migrate the endpoint to QAR. As I was setting up the QAR endpoint, I discovered the the jena/joseki tools are hard-coded to use the "OracleSemDS" data source. Therefore, I cannot have 2 endpoints running in the same OC4J instance where one points to DEV and the other points to QAR.
    Therefore, I need to create a second instance of OC4J that specifies QAR as the "OracleSemDS" data connection. Can someone tell me how to do that?
    Thanks,
    Phil

    jphilb wrote:
    I am using OC4J standalone to deploy a sparql endpoint using jena, joseki, and the oracle jena adaptor. OC4J is running on the same server as my DEV and QAR databases. The current instance of OC4J is using a data source called "OracleSemDS" to connect to my DEV database. We are now ready to migrate the endpoint to QAR. As I was setting up the QAR endpoint, I discovered the the jena/joseki tools are hard-coded to use the "OracleSemDS" data source. Therefore, I cannot have 2 endpoints running in the same OC4J instance where one points to DEV and the other points to QAR.
    Therefore, I need to create a second instance of OC4J that specifies QAR as the "OracleSemDS" data connection. Can someone tell me how to do that?
    Thanks,
    Philgday Phil -- Melli Annamalai from the Oracle Server PM team pointed me to your question. I was an OC4J Product Manager before we bought out BEA and I swapped over to WLS -- so I have some information below that hopefully is of assistance to you.
    Since you are using OC4J standalone, the simplest way to get a second instance, is to just clone the existing, whole directory structure of your working OC4J instance into another directory, and configure/run it from that directory. To map the OracleSemDS to another target (QAR) you change the data-sources.xml file in the second instance. If you want to run them concurrently on the same server, you'll need to change the ports in the second instance so they have unique values: default-web-site.xml, rmi.xml, internal-settings.xml, jms.xml. These additional ports can be specified on the command line as a Java property string, albeit this is not a documented nor supported feature. See http://buttso.blogspot.com/2007/02/specifying-oc4j-standalone-ports-from.html for more information.
    Now as an example, assuming you have your OC4J you want to clone in the dev directory, and now want to create a second instance of OC4J in qar, I'd do this:
    cp -rp dev/* qar
    cd qar/j2ee/home/config
    vi data-sources.xml
    (edit any other files if needed to alter ports) ...
    cd ..
    java -jar oc4j.jar That should work, and should create the second instance using the current configuration (ie dev) as its basis (including deployed apps). Of course, you'll need to manage them independently from here as they really are just two separate OC4J standalone instances.
    Now what else you could potentially do is to create a secondary configuration file subset only which only changes the necessary files to support running the second instance. In this case, you'd make a copy of the j2ee/home/config/server.xml, j2ee/home/config/application.xml and j2ee/home/config/data-sources.xml files, renaming them to qar-server.xml, qar-application.xml and qar-data-sources.xml.
    Then do the following:
    1. In qar-server.xml file, you'd change the global-application application to point at qar-application.xml instead of application.xml:
    <global-application name="default" path="qar-application.xml" parent="system" start="true" />
    2. Change qar-application.xml so that it used the qar-data-sources.xml file:
    <data-sources path="qar-data-sources.xml" />
    3. Change qar-data-sources.xml file so it points to the qar database target.
    4. Start OC4J, telling it to use qar-server.xml instead of the default server.xml
    D:\java\oc4j-10135-prod\j2ee\home>java -jar oc4j.jar -config config/qar-server.xmlI think you could run into some concurrency problems if you try and run the two instances simulataneously, but in general it should be OK I think if you run them separately.
    Another solution here is look at how the application specifies/uses datasources. The hard coding of the data-source name into the applicaiton via a direct JNDI lookup works, but restricts the flexibility you as a deployer/administrator has.
    If the application used the Java EE resource-ref approach to look up and use the datasource, then the actual JNDI name of the datasource the application uses it totally abstracted from the actual physical name of the datasource that is created on the container -- so you'd have DEVDS and QARDS as physical datasources configured and running on OC4J -- and what happens is that as you deploy the application, you essentially map its lookup and use of OracleSemDS (which would need to change to a java:env/ namespace) to the physical datasource you want it to use (DEVDS or QARDS for example). You can easily alter this post deployment by changing the generated orion-application.xml file to point at the alternate physical datasource. Following that, you can also create separate deployment plans which map the app to the different datasources, then feed this in with the deployment operation so that the correct mapping to either DEVDS or QARDS is done during the deployment process.
    I don't know what scope you have to change the application so the above may not be possible -- you'd need to change the lookup code so that it uses the Java EE reference model and change the meta-inf/application.xml to add the corresponding resource-ref entry which declares the logical datasource name that needs to be mapped on deployment.
    Another approach here may be to use an application embedded datasource, where you put a data-sources.xml file into the application archive itself (along with an orion-application.xml file which references it) whereupon at deployment time, a datasource specific to the application will be created. In this manner, you'd have say two copies of the same application to deploy (DEV, QAR) which contain different data-sources.xml file. From memory, each application will have its own JNDI namespace so their datasources can co-exist with the same names, with application level data-sources overriding server level ones. Since you'll most likely want to keep the same context-root for the web modules (which has to be unique per server) you'll generally need to run just one of the applications at a time. I'd do this like this: deploy one, stop it, deploy the other, stop it, then start only the one you need to test. In that way, you'll have one copy of the application running (either DEV or QAR) and one datasource definition running within it.
    Here's some documentation links to get you going:
    Managing Application Lifecycle (start, stop): http://download.oracle.com/docs/cd/E14101_01/doc.1013/e13978/adminclient.htm#BABHJAFE <-- describes admin_client.jar but general principles apply to ascontrol management of application.
    Packaging and Testing Applications: http://download.oracle.com/docs/cd/E14101_01/doc.1013/e13979/packag.htm#BHCFBEEC
    Application Level DataSources: http://download.oracle.com/docs/cd/E14101_01/doc.1013/e13975/datasrc.htm#CHDIBFHG
    Using Deployment Plans: http://download.oracle.com/docs/cd/E14101_01/doc.1013/e13980/deployplan.htm#CHDFEFAE
    cheers
    -steve-

  • How do I replace a single instance of a string?

    I'm trying to replace a certain string in a text file. The problem is, there is other data in the text file that is being affected by the replace() function, since replace() replaces each substring that fits the pattern it's looking for. I want my replacement function to target one single instance of something, at one specific instance, and to leave the rest of my text alone.
    Any thoughts on how I'd do that?

    I don't see how to do it with a String direclty without making silly copies.
    But it seems you could use a StringBuilder and its method replace(int start, int end, String str).
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html#replace(int,%20int,%20java.lang.String)

  • How to deal with the old instances of any class?

    Hi,
    I have a doubt regarding the old instances of any class. Currently i am designing a game in java swing. I have a new game button, which creates a new game instance of the game class. There might be conditions that the old game is finished and user has clicked for new game button. The old game instance is no more useful for me. In fact that old instance must be deleted from the memory and a new instance should reside in the memory until user again proceed for a new game.
    I want to ask that how to destroy old instances of any class?
    Thank you.

    rp0428 wrote:
    All you can do is remove any reference to it:
    myGame = null;If that was the last reference then it will be eligible for garbage collection and it MAY be collected.And note that there's rarely a good reason to do this. In most cases, simply letting things go out of scope when the method that uses them ends is sufficient. About the only time you need to explicitly set something to null like this is, for example, if you're implementing a collection class and you want to remove an entry.

Maybe you are looking for