How to pass a session from a class to another class
Mattia Merenda
Ranch Hand
Joined: Dec 14, 2006
Posts: 47
posted
0
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
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
posted
0
What error are you getting ,when you try to create the session?
HttpSession Object which is associated with the request by call req.getSession. the argument true forces the creation of a new session if the request doesn't contain a valid session key.
req is not initialized or in other words you have not obtained the refrence of req here and hence using req will obviously will give you a NullPointerException.
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.
Mattia Merenda
Ranch Hand
Joined: Dec 14, 2006
Posts: 47
posted
0
Hi, If I cannot get the request in this way,how can I get from the container the informations that I need? Thanks, Mattia
1. You write the servlet 2. You map the servlet in the web.xml 3. You call the servlet
Mattia Merenda
Ranch Hand
Joined: Dec 14, 2006
Posts: 47
posted
0
Hi, Is it mandatory to write the servlet?Is there another way? Because I already have my Action class instead of the servlet.I don't know if this can create other problems. Thanks, Mattia
Look on line 35 of the class ClusterTotals. One of the objects used on that line is null when you aren't expecting it to be. Find out what that object is, find out why it's null, and fix it.
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.
- Brent
Mattia Merenda
Ranch Hand
Joined: Dec 14, 2006
Posts: 47
posted
0
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
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: How to pass a session from a class to another class