Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

variable session problem


  • Please log in to reply
2 replies to this topic

#1 commissioner


    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 08 April 2011 - 08:41 PM

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.

  • 0

#2 wim DC

wim DC


  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 09 April 2011 - 12:13 AM

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

  • 0

#3 commissioner


    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 09 April 2011 - 01:20 PM

thanks excellent
  • 0

Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download