This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass a session from a class to another class

 
Mattia Merenda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting ,when you try to create the session?
 
Mattia Merenda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic