File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to pass a session from a class to another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to pass a session from a class to another class" Watch "How to pass a session from a class to another class" New topic
Author

How to pass a session from a class to another class

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
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: 2308
What error are you getting ,when you try to create the session?


Rahul Bhattacharjee
LinkedIn - Blog
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
In my class for the chart I try to get session in this way:

HttpServletRequest req;
HttpSession sess = req.getSession();

I get this error when I open the page:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

root cause

java.lang.NullPointerException
com.myapp.struts.ClusterTotals.<init>(ClusterTotals.java:35)
org.apache.jsp.chartcluster1_jsp._jspService(chartcluster1_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

I hope this helps for a solution
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Mattia Merenda:
I am getting errors.So,how to get the session from the Action class in my new class for the charts?


it would be more understandable for a person to inspect on error, exception and code snippet instead of getting errors sentence.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

try with this.


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.

See Java Doc for getSession
[ January 24, 2007: Message edited by: Saif uddin ]
aslam parveez
Ranch Hand

Joined: Apr 25, 2006
Posts: 47

In my class for the chart I try to get session in this way:

HttpServletRequest req;
HttpSession sess = req.getSession();


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.
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi,
I tried to put

HttpSession sess = req.getSession(false);

and

HttpSession sess = req.getSession(true);

I think Aslam is right.The problem is that "req" is not initialized.So how can I solve this problem now?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
Hi,
If I cannot get the request in this way,how can I get from the container the informations that I need?
Thanks,
Mattia
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have you written a servlet?

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
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm going to move this to our Struts forum.
Those folks should know if Struts passes a reference to the session object to the action classes.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
An instance of HttpServletRequest is passed to the execute method of a Struts Action class when it is called. Use this to get the session.

More directly related to the error you're encountering: The lines of the stack trace that are most important are these:
java.lang.NullPointerException
com.myapp.struts.ClusterTotals.<init>(ClusterTotals.java:35)

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
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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
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:

HttpServletRequest req;
HttpSession sess = req.getSession();
String query = (String) sess.getAttribute("sqlTotal");
....

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
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://aspose.com/file-tools
 
subject: How to pass a session from a class to another class
 
Similar Threads
Problem with session
DispatchAction problem
How to pass a session from an Action class to another class
Problem with 2 buttons in the same form
how to create the .rptdesign file using birt api