1

I have a Servlet thats run and takes in data from a web page. Later on in a different set of tasks I want to access this data from a standard Java class and use the data, how would i go about this? Can I save the data anywhere for access?

I have code like this :

String name = request.getParameter("username");

And then I tried to set it as an attribute and pass it :

getServletContext().setAttribute("com.mycompany.app-param", "name");

Then in the next class I tried to access the context to get the varible, but no matter what I tried I got server error 500's, or null or Server = null. i don't think it's able to pick up the context properly :

value = getServletContext().getAttribute("com.mycompany.app-param");

Does anyone know how I can access the context created previously and get its variable?

3
  • 1
    I'm not sure I understand your question well, most Java Enterprise tutorials have at least a section where data are received from the user and somehow stored, eg in a database? Plus, a servlet is a Java class like any other so it can do file io. What exactly do you require assistance with? Commented Jul 19, 2012 at 12:58
  • My servlet is running on app engine and reads in the data when its first run. i tried passing the context of the servlet to the java class but i kept getting errors. if you pass me on one of those tutorials I'd love to have a look at it, maybe I'm missing something... Commented Jul 19, 2012 at 13:04
  • It would be most helpful if you could show with code what you actually try to do ("passing the context" is a bit broad), and the error messages ("keep getting errors" which errors? show the stacktrace or at least the error message) you are getting. Commented Jul 19, 2012 at 13:26

6 Answers 6

1

See Is getServletContext() officially supported in GAE? - on gae it's not guaranteed to work the way you want it to work (eg to pass on info).

You should use Session or Request scoped attributes instead, see here here and here to get you started. Which one you use depends on the required life time and visibility of the saved attributes (Request < Session < Application)

Sign up to request clarification or add additional context in comments.

1 Comment

Actually ended up using this method, but thank you everyone else for your help. Your answers were all helpful and all deserve a thumbs up. Thanks to all! :)
1

Yes, like in a database table? Or even a file?

Sample code:

https://www.ibm.com/developerworks/mydeveloperworks/blogs/wasdev/entry/servlet_jdbc_sample1?lang=en

1 Comment

Yes that would be feasible, could you direct me to an example implementation? Thanks
1

You can make use of a DTO with a set of getters(getting data to java class) and setters(setting data from web page) and maintain it a global scope so that you can access it later using the Java class...Hope this helps!!!

2 Comments

Do you have a sample implementation I could look at? Thanks! :)
Cool, I can try this. How would I maintain a global scope?
1

You can save that data in some file

Or

You can provide Class having all properties in UI and create a object for that class and serialize that object at that moment after that when ever you want that data you can desrialize that object.

Serialization link http://www.tutorialspoint.com/java/java_serialization.htm

Comments

1

Here you go...

public class DummyDTO {
    private String name = null;
    private String age = null;

    // getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
} 

public class MyServletClass extends HttpServlet {
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
        DummyDTO dummyDTO = new DummyDTO();
        dummyDTO.setName(request.getParameter("name"));
        dummyDTO.setAge(request.getParameter("age"));
        AnotherClass.setValues(dummyDTO);
    }




    public class AnotherClass {

        String name = "";
        String age = "";

        public static void setValues(DummyDTO dummyDTO) {

            name = dummyDTO.getName();
            age = dummyDTO.getAge();
        }

    }   

3 Comments

Check if this approach helps you...rather than going for context...if retrieving and passing around values is your intent..
Sorry for the wrong suggestion Matt...this approach will not work because...if you have several concurrent requests to your servlet, the second one will overwrite the data stored by the first one in the static fields ..so it is not thread safe...
Thanks for your help all the same, really appreciated! :)
1

you can write a data access layer with one dao which will persist the data in database . If your requirement is to use data only at session scope then you can save that data in session.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.