Hi everybody, I am developing a web application with Struts. I have a form where the user inserts some values and in Action class I create the query to get the results from a database. When I submit the form I get a table with these results. Now I want to make some chart with these results on the table. I am using Cewolf for this purpose. To make the chart I must develop a Java class where I need to create the same query that I have in the Action class. For this reason,I saved all the parameters in the form in a session in my Action class. Now in the class to make the chart I must call the session that I saved previously. And for the moment,I am getting errors.So,how to get the session from the Action class in my new class for the charts? I need a fast answer if it is possible. Thanks in advance for this. Mattia
You don't, the container creates it and passes it to the servlet when it gets a request from the client. Without a request from the client you can't have a request object, and therefore can't access the session.
Joined: Dec 14, 2006
Hi, If I cannot get the request in this way,how can I get from the container the informations that I need? Thanks, Mattia
Merrill answered your question, but just to backup a bit...your example shows a basic principle of Java that you need to understand.
All your first line did was to create a reference to an object. It did not create the object so the second line of code is going to throw a NullPointerException when you try to call a method on a null reference ("reference" and "pointer" are basically the same thing). It does not matter that req is of type HttpServletRequest. It could very well be String, ArrayList, or any other object type.
Joined: Dec 14, 2006
Hi, I add a part of the code of my Action class and my class that I use to make the chart where I think there is the problem:
In my ClassAction.java I save in the session the query that I create:
HttpSession session = request.getSession(); String sql = "SELECT ..... FROM .... WHERE username LIKE '%" + Username + "%' AND total = " + Ttotal +"...."); session.setAttribute("sqlTotal",sql);
In ClusterTotals.java I try to call the session to get the query in this class:
I get the NullPointerException error.I think the problem is that the request is not initialized.I should fix it in a way I will be able to get the query of my Action class,but I don't know how and I also don't know if this is the right way. I think the code is clear to understand my problem. Thanks, Mattia
Joined: Feb 15, 2005
Another important point to understand in J2EE programming is this: You can't instantiate an HttpServletRequest object. It must be instantiated by the container and passed to your Class.
It's clear from the stack trace that you're calling a method on ClusterTotals from a JSP. Since it's easy to get an object from the session in a JSP, I'd suggest you get the query as a String in the JSP and then pass it to the method of the ClusterTotals class. Example:
Note that "session" is a pre-defined variable in a JSP. You don't have to do anything to define or populate it.
subject: How to pass a session from a class to another class