4 Replies Latest reply on Mar 31, 2010 7:46 AM by marcelstoer

    getServletContext() fails in JSP

    marcelstoer
      Calling getServletContext() in a JSP miserably fails in WebLogic:

      weblogic.servlet.jsp.CompilationException: Failed to compile JSP /jsp/error.jsp
      error.jsp:28:87: The method getServletContext() is undefined for the type __error


      Any ideas for workarounds?

      Cheers,
      Marcel
        • 1. Re: getServletContext() fails in JSP
          Jay SenSharma MiddlewareMagic
          Hi,

          Which version of WLS are you using?
          Can you please paste the Code snippet of your *"error.jsp"*....Apart from this ..Please try to run the APPC Task with attribute "keepgenerated=true" Sothat we can get more information which explicitly compiling our JSP page...

          http://jaysensharma.wordpress.com/2010/02/08/weblogic-specific-appc-ant-task/

          <target name=”appc”>
          <wlappc source=”${application.dir}”
          keepgenerated=”true”
          verbose=”true”
          output=”${application.dir}_Output”/>
          </target>

          .
          .
          Thanks
          Jay SenSharma
          • 2. Re: getServletContext() fails in JSP
            marcelstoer
            We have WLS 10.3.1.0.

            What makes you assume I use the APPC Task? In fact, we're not ;-) I use the autodeploy feature on our integration host.

            Where does WLS store its JSP .java and .class files? Does it have something like a "work" folder (as Tomcat) has? Btw, this problem does not occur in Tomcat.

            <snippet>
            <c:set var="exception" value="${requestScope['javax.servlet.error.exception']}" />
            ...

            <%
            boolean isStackTraceEnabled = false;
            WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            if (context != null) {
            FeatureConfig featureConfig = (FeatureConfig)context.getBean("featureConfig",FeatureConfig.class);
            if (featureConfig != null) {
            isStackTraceEnabled = featureConfig.getIsStacktracesEnabled();
            }
            }

            %>
            ...

            <p>${exception.message}</p>
            </snippet>

            -> WebApplicationContextUtils is from Spring.
            • 3. Re: getServletContext() fails in JSP
              Jay SenSharma MiddlewareMagic
              Hi,

              Please use *"application"* the implicit Variable for JSP Pages to get the ServletContext reference. Reason the JSP Pages doesn't directly extend the javax.servlet.http.HttpServlet....It extends JspBase class...which doesnt have that method. The JSPc Implementation of WebLogic is different from Tomcat.
              <BR>
              The ServletContext variable is already available for all the JSPs according to JSP specification...we can directly pass the variable "application" where ever we want to use ServletContext reference.

              Means U need to Just replace the following line:
              <font color=red>WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());</font>
              <BR><BR>
              With
              <font color=green>WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( application );</font>
              ======================

              <font color=maroon>What makes you assume I use the APPC Task?</font><BR>
              I didnt say that u are using APPC...I wanted you to run APPC task separately on the Application so that you can see the issue with detailed description ...in the APPC output...And even the *.java files wjhich gets genaerated by Weblogic APPC task after compiling the *.JSP files.


              <font color=maroon>Where does WLS store its JSP .java and .class files? Does it have something like a "work" folder (as Tomcat) has? Btw, this problem does not occur in Tomcat.</font>
              As soon as you run the APPC task on the Application with Keepgenerated =true option ...you will see that the *.java files gets generated inside your WEB-INF/classes directory.
              If you dont want to run the APPC separately still if you want to see the *.java files of your JSPs then Please place <keepgenerated>true</keepgenerated inside your "WEB-INF/weblogic.xml" file.

              .
              .
              Thanks
              Jay SenSharma
              • 4. Re: getServletContext() fails in JSP
                marcelstoer
                Wow, great answers. Thanks a lot!

                Marcel