Getsy
Getsy

Reputation: 4905

Java: ServletContext equivalent in Plain Java

I have the following servlet code to store a data for a specific reason and later I retrieve the same data in another scenario in a project. It works fine as expected.

// setting 
   ServletContext context = request.getSession().getServletContext();
    context.setAttribute("imageData", data);

    // retrieving ...
    ServletContext context = request.getSession().getServletContext();
byte[] data = (byte[])context.getAttribute("imageData");

Now, In an another project, where I use plain java program, where I want to store data similar to this servlet logic. I tried using the same code in plan java project, but it throws error, not accepting this servletcontext in plan java file.

Could someone please help, 1.) what is the equivalent in plan java to store like this temporarily or 2.) How to make that servletconext code working in plain java ?

Thank you in advance!

Upvotes: 0

Views: 171

Answers (2)

Guy Bouallet
Guy Bouallet

Reputation: 2125

You can consider using simply a HashMap in the applet context. If you need to use the same code in applet and web contexts, add a layer of abstraction and two implementations to manage the attributes depending on the context.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240900

How about simple HashMap<String, Object>

Upvotes: 1

Related Questions