Problem with session
good morning.
I have a problem with sessions.
I磛e created a java class to access to several session variables that I have defined in a jsp page.
I can read these session variables doing:
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;
publicclass Configextends HttpServlet{
HttpSession session;
String valor =null;
public Config (HttpSession session){
this.session = session;
valor = (String)this.session.getAttribute("Var_TipoAgeModel");
}
public String getValueSession(){
return valor;
}
}
But now I want be able to modify the value of different session variables and I磎 trying:
import java.util.*;
import analisisEspectral.*;
import java.text.*;
import java.lang.*;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;
publicclass Functions_AE{
HttpSession session;
public Functions_AE( HttpSession session){
this.session = session;
System.out.println("\nStart constructor\n");
System.out.println("\n-->"+this.session.getAttribute("MyVar")+"<--");
System.out.println("\nEnd constructor\n");
}
publicstaticvoid main(String[] args){
HttpSession session;
session.setAttribute("MyVar","valor");
Functions_AE frame =new Functions_AE(session);
System.out.println("\nEND program");
}
}
And When I try to compile I obtain:
variable session might not have been initialized
session.setAttribute("MyVar","valor");
^
How I could solve this?
Thanks
> variable session might not have been initializedHttpSession session;session.setAttribute("MyVar","valor");It's totally correct. I'd see a NullPointerException coming if that one had slipped past the compiler.
Your Config class doesn't make any sense. If you subclass HttpServlet you need to override at least one of: doGet, doPost, doPut, doDelete etc.To return or create a session associated with a request you call the getSession() or getSession(boolean create) method of
>It's totally correct. I'd see a NullPointerException coming if that one had slipped past the compiler. Sorry, I do not understand very well that what you say to me.The code is correct?If it isn磘 ..., what I can do?Thank you very much.
> Your Config class doesn't make any sense. If you
> subclass HttpServlet you need to override at least
> one of: doGet, doPost, doPut, doDelete etc.
yes, sorry. I know it and I have modify this (I caused that this was a Servlet to test.). Now I do:
public class Config {
HttpSession session;
String valor = null;
public Config (HttpSession session){
this.session = session;
valor = (String)this.session.getAttribute("Var_TipoAgeModel");
}
public String getValueSession(){
return valor;
}
}
> To return or create a session associated with a
> request you call the getSession() or
> getSession(boolean create) method of
> HttpServletRequest.
My problem now Is diferent. I have a java class (not a servlet. This is not a Servlet, reason why I cannot call the getSession() or getSession(boolean create) no?) and I want to create a session and different session variables, it is by this reason why I do this:
import java.util.*;
import analisisEspectral.*;
import java.text.*;
import java.lang.*;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;
public class Functions_AE{
HttpSession session;
public Functions_AE( HttpSession session){
this.session = session;
System.out.println("\nStart constructor\n");
System.out.println("\n-->"+this.session.getAttribute("MyVar")+"<--");
System.out.println("\nEnd constructor\n");
}
public static void main(String[] args) {
HttpSession session;
session.setAttribute("MyVar","valor");
Functions_AE frame = new Functions_AE(session);
System.out.println("\nEND program");
}
}
> Sorry, I do not understand very well that what you
> say to me.
> The code is correct?
> If it isn磘 ..., what I can do?
It's not correct. You're not initializing the session variable. The compiler told you and the two lines of code make that very clear, too.
How do you want to set its attributes if no session instance exists?
> How do you want to set its attributes if no session> instance exists?I understand, but how can I create a new session instance if this isn磘 a Servlet in which I can associate a request?thanks
> I understand, but
> how can I create a new session instance if this isn磘
> a Servlet in which I can associate a request?
If there's no servlet: where is a session supposed to come from in the first place?
If it's just for testing, you can simply create a HttpSession implementation yourself and instantiate it.
> My problem now Is diferent. I have a java class (not
> a servlet. This is not a Servlet, reason why I
> cannot call the getSession() or getSession(boolean
> create) no?) and I want to create a session and
> different session variables
The servlet container uses the HTTPSession interface to create a session between an HTTP client and an HTTP server. I don't see from your code why you are trying to use it at all.
Hii Conmpiler is saying right thing.HttpSession session is a local variable and you must initalise the local variabled.
> My problem now Is diferent. I have a java class (not
> a servlet. This is not a Servlet, reason why I
> cannot call the getSession() or getSession(boolean
> create) no?) and I want to create a session and
> different session variables
You can't create an HttpSession if you do not have a ref to HttpServletRequest.
But you stated earlier that you wanted to access "several session variables that are defined in a jsp page".
In JSP pages, an implicit object called session is created automatically, unless the session is disabled in the page directive.
I can't really comment any further, given the fact that I do not understand what you're trying to accomplish exactly and why.