如何在ajax请求页面中获取会话变量?

时间:2012-09-07 08:46:34

标签: java javascript ajax jsp tomcat

我使用以下代码使用ajax将请求发布到另一个jsp页面。我已经在发出请求的当前页面中有一个会话变量(stdId)。

xmlhttp.open("POST","ajaxAnsRun.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("notId="+we+"");

现在在ajaxAnsRun.jsp页面中,我使用了以下代码来获取会话变量。

<jsp:useBean id="obj" class="homePack.home" />
<%
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);
%>

我的服务器出现以下错误

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)

顺便说一句,我正在使用tomcat。

如何在ajaxAnsRun.jsp页面中获取会话变量。?

2 个答案:

答案 0 :(得分:1)

这里的问题是stdId没有定义。这就是它给出编译时错误的原因。 将stdId定义为String,如下所示

String stdId=String.valueOf(session.getAttribute("stdId"));

答案 1 :(得分:0)

您的代码未在ajaxAnsRun.jsp中声明stdId变量。它只声明notId变量。你的代码应该是。 这就是编译错误发生的原因。

<jsp:useBean id="obj" class="homePack.home" />
<%
String notId=request.getParameter("notId");
String stdId = null;
if(request.getSession(false).getAttribute("stdId") == null)
    {
        out.println("Error");
    }
else
    {
        stdId=String.valueOf(session.getAttribute("stdId"));
    }

String result="";
result= obj.hideAnsNotify(notId,stdId);
%>