Why not getervletcontext found?

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

cannot find symbol symbol: getServletContext () method location: javax.servlet.http.HttpSession interface 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(); } 

Do you have 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 is slightly different from the code shown on this web . I donโ€™t know how this can affect my project, but I like to know if there is a different approach to getting the same result as when using the 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> 
+8
java spring-mvc servlets
source share
1 answer

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

So the only explanation is that your project checks the code for the old version.

getServletContext() also present in the Spring Controller class, which is apparently used. So instead of session.getServletContext().getRealPath( "/" ) you will be fine with getServletContext().getRealPath( "/" )

Edited January 30: Clearing Jasper Reports jar files

I checked that jasperreports-3.7.1-project.zip has an old version of servlet.jar. I recommend the following:

  • Delete all the jar files that you added from the jasperreports-3.7.1-project.zip lib folder, but save the jar files from the "dist" folder.

  • Add the jar file one by one based on compilation error messages. Do not add the jar file, which is also available in the TOMCAT-HOME / lib folder, and do not add the Spring jar file. Since you know that the jasper project project has old jar files, first look to see if these jars provide netbeans, and then do not try to use the latest versions from other repositories, for example http://repo1.maven.org/maven2/ . Spring Downloading a framework with dependencies also has several common files that you can use.

  • Check out any online resource for more information on the required jar files. The 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 similar related to version 3.7. one.

+4
source share

All Articles