title: How to get session variable in ajax request page?
body: <p>I have used the following code to post a request to another jsp page using ajax. Im already having a session variable (stdId) in the current page from where the request is made.</p>
  
  <pre><code>xmlhttp.open("POST","ajaxAnsRun.jsp",true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("notId="+we+"");
  </code></pre>
  
  <p>Now in the ajaxAnsRun.jsp page i have used the following code to get the session variable.</p>
  
  <pre><code>&lt;jsp:useBean id="obj" class="homePack.home" /&gt;
  &lt;%
  String notId=request.getParameter("notId");
  if(request.getSession(false).getAttribute("stdId") == null)
      {
          out.println("Error");
      }
  else
      {
          stdId=String.valueOf(session.getAttribute("stdId"));
      }
  
  String result="";
  result= obj.hideAnsNotify(notId,stdId);
  %&gt;
  </code></pre>
  
  <p>I got the following error in my server </p>
  
  <pre><code>2012-09-06 17:22:25 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
  org.apache.jasper.JasperException: Unable to compile class for JSP
  
  An error occurred at line: 2 in the jsp file: /profiles/Home/ajaxAnsRun.jsp
  
  Generated servlet error:
  [javac] Compiling 1 source file
  
  C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\Indus\profiles\Home\ajaxAnsRun_jsp.java:66: cannot find symbol
  symbol  : variable stdId
  location: class org.apache.jsp.ajaxAnsRun_jsp
      stdId=String.valueOf(session.getAttribute("stdId"));
      ^
  
  
  
  An error occurred at line: 2 in the jsp file: /profiles/Home/ajaxAnsRun.jsp
  
  Generated servlet error:
  C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\Indus\profiles\Home\ajaxAnsRun_jsp.java:70: cannot find symbol
  symbol  : variable stdId
  location: class org.apache.jsp.ajaxAnsRun_jsp
  result= obj.hideAnsNotify(notId,stdId);
                              ^
  2 errors
  
  
  at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:130)
  at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:293)
  at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:340)
  at org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
  at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:474)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:184)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
  at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
  at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
  at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
  at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
  at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
  at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
  at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
  at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
  at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
  at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>btw im using tomcat.</p>
  
  <p><strong>How can i get the session variable in ajaxAnsRun.jsp page.?</strong></p>