Why getservletcontext isn't found?

2020-07-07 09:59发布

问题:

I'm trying to use getServletContext().getRealPath( "/" ) , but I keep getting this error:

cannot find symbol symbol : method getServletContext() location: interface javax.servlet.http.HttpSession String path = session.getServletContext().getRealPath("/") + "layout/tiles/" + reportPath ;

public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response ) throws Exception {

        session = request.getSession();
        Map params = new HashMap();
        String reportPath = "maintenance/jasper/report01.jasper";
        exportToPDF( reportPath , response, params );

        return null;
    }

    protected void exportToPDF( String reportPath , HttpServletResponse response, Map jasperParams ) throws Exception {

            String path = session.getServletContext().getRealPath( "/" ) + "layout/tiles/" + reportPath ;

            if ( !new File( path ).exists() ) {
                throw new Exception( "The path doesn''t exist. </br>" + path );
            }
            InputStream input = new FileInputStream( path );

            jasperParams.put( "REPORT_LOCALE", Locale.US );

            JasperPrint jasper = JasperFillManager.fillReport( input , jasperParams, new JRBeanCollectionDataSource(Vehicles) );

            response.setContentType( "application/pdf" );
            ServletOutputStream output = response.getOutputStream();

            JRExporter exporter = new JRPdfExporter();

            exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasper );
            exporter.setParameter( JRExporterParameter.OUTPUT_STREAM, output );

            exporter.exportReport();
            output.close();


    }

Have you any idea why this is happening ?

Thanks Ritesh, I did what you told me, but now I get a new message

------EDIT--------

checking my dispatcher-servlet.xml I found that it's kind of different from the code shown on this web . I don't know how it could affect my project, but what I do like to know if there's a different approach to getting the same result as using session.getServletContext().getRealPath( "/" )

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
              value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

回答1:

getServletContext() was added in Servlet 2.3. It was not there in 2.2, see Servlet 2.2 javadoc

So only explanation is that your project is validating code against old version.

getServletContext() is also there in Spring's Controller class that you seem to be using. So instead of session.getServletContext().getRealPath( "/" ), you will be fine with just getServletContext().getRealPath( "/" )

Edited 30 Jan: Jasper Reports jar files cleanup

I have verified that jasperreports-3.7.1-project.zip has old version of servlet.jar. I recommend following:

  1. Remove all jar files that you added from lib folder of jasperreports-3.7.1-project.zip but keep jar files from "dist" folder.

  2. Add jar file one by one based on compilation error messages. Please do not add any jar file that is also available in TOMCAT-HOME/lib folder and do not add any Spring jar file. Since you know that jasper reports project has old jar files, first see if netbeans provides those jars, if not then try with the latest versions from other repositories such as http://repo1.maven.org/maven2/. Spring framework download with dependencies also has several common files that you can use.

  3. Check any online resource to get more information about required jar files. Following link describes integration with jasper reports version 1.2.5 in netbeans: http://developers.sun.com/jsenterprise/archive/reference/techart/jse8/jasper_reports.html But you need something like that related to 3.7.1 version.