Jump to content

variable session problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
Hi everybody. i have run my program in jsp but i have move it to servlet. during compilation, the following error ensued

C:\Tomcat 6.0\webapps\test\WEB-INF\classes>javac Test.java  

    Test.java:26: cannot find symbol  

    symbol  : variable session  

    location: class Test  

    String name = session.getAttribute("name").toString();  

                      ^  

    1 error  


how do i define variable session or how do i fetch the session from the request instance. please give example. thanks

Edited by Roger, 10 April 2011 - 03:11 PM.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Is Test a servlet class? or just a simple class?
Servlet solution (so you need the session in a doGet or doPost or doX method:

HttpServletRequest httpRequest = (HttpServletRequest) request;    //the request variable should be part of your method input variables. 

HttpSession session = httpRequest.getSession();


In other classes, where you don't have such a request variable, you can try:

FacesContext facesContext = FacesContext.getCurrentInstance();

HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);   //false in the end, or you create a new session



#3
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
thanks excellent




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users