Results 1 to 2 of 2
  1. #1
    commissioner is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default variable session problem

    Hi everybody. i have run my program in jsp but i have move it to servlet. during compilation, the following error ensued
    [code]
    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
    [code]

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

  2. #2
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default reply to variable session problem

    hi commissioner,

    Your servlet is showing such an error because its cannot find any variable called session. In a jsp you need not to declare a session variable explicitly, but in a servlet you need to do it!

    So before ou write :
    Java Code:
    String name = session.getAttribute("name").toString();
    declare your session variable first and then use it -

    Java Code:
    HttpSession session=request.getSession(false); // you may use true instead of false
    String name =(String) session.getAttribute("name");
    Try this...it'll surely work!..

    good luck! :)

Similar Threads

  1. get Object contents from session variable
    By bekir in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-05-2011, 02:13 PM
  2. Accessing session variable from Java Class
    By Rohit Pokhrel in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-08-2010, 05:07 PM
  3. Session Variable corruption example..
    By joe131 in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2010, 03:51 PM
  4. how to use user name as a session variable in jsp
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-05-2010, 10:09 AM
  5. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •