9

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 ; enter image description here

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

enter image description here

------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>
eddy
  • 4,373
  • 16
  • 60
  • 94
  • Can you check out this document http://netbeans.org/kb/68/web/quickstart-webapps-spring.html and see if anything is different in your project setup? – Ritesh Jan 30 '11 at 13:18
  • Which version of Jasper reports are you using and which jars of Jasper reports have you added in your project? – Ritesh Jan 30 '11 at 17:21
  • @Ritesh-JasperReports 3.7.1 and ... this is quite embarrassing, since I didn't know which jars to add, I added all the jars included in the "lib" and "dist" folders that come with the jasperreports-3.7.1-project.zip. – eddy Jan 30 '11 at 18:05
  • Please see my edited answer below. – Ritesh Jan 30 '11 at 23:20

1 Answers1

5

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.

Ritesh
  • 7,472
  • 2
  • 39
  • 43
  • *"validating code against ..."* - more likely, it is *compiling* against an old version of the APIs. – Stephen C Jan 30 '11 at 01:29
  • Sorry, I'm new to java. Could you please tell me how I can change the version? where can I find this jar? – eddy Jan 30 '11 at 01:41
  • From question (and screenshot), it is not clear if it is a compilation error. There is no information on how the project is being built. If you are using Eclipse IDE then right click on project name, click on properties, select 'Project Facet' and change the version of 'Dynamic Web Module'. That will fix the validation error in IDE. – Ritesh Jan 30 '11 at 02:21
  • @Ritesh- I'm using netbeans 6.8 and tomcat 6.0.29. When I try to generate the project, this what I get now: "cannot find symbol symbol : method getServletContext()" – eddy Jan 30 '11 at 04:41
  • @Ritesh- I don't know how to say thank you for your help. I did what you told me and turned out to be that the only jar I needed was "jasperreports-3.7.1.jar".I didn't even need to add "iText-2.1.0.jar", which I thought was required when exporting pdf. – eddy Jan 31 '11 at 03:38