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>