Jump to content
We've recently updated our Privacy Statement, available here ×

Images not working in Webapp


2004 IR Help

Recommended Posts

By: Philippe Damerval - damerval

Images not working in Webapp

2005-02-15 16:29

Hi,

I took the webapp sample module and installed it under Tomcat and it worked beautifully. I then designed my own report in iReport, ran it and it ran beautifully. When I modified the code of the HtmlServlet class to call my report instead of the sample report, it works fine except that the report has no images. I don't use explicit images in my design, however as you know the HTML export libraries use images as part of the rendering. having no images, my HTML report looks crummy.

 

As far as I can see, the only differences between my servlet and the sample html servlet are as follows:

 

- The design uses a different dataset so looks different

- The servlet is in a different package

- It uses a live connection instead of a DataSource for data

- It does not use the REPORT_TITLE parameter therefore does not pass it.

 

Here is the code of my servlet. Please tell me if anything looks wrong:

 

package adfg;

 

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.File;

import java.util.Map;

import java.util.HashMap;

import java.sql.DriverManager;

import java.sql.Connection;

 

import net.sf.jasperreports.engine.*;

import net.sf.jasperreports.engine.util.*;

import net.sf.jasperreports.engine.export.*;

 

 

/**

* User: PDamerval

* Date: Feb 14, 2005

* Time: 1:16:32 PM

* To change this template use File | Settings | File Templates.

*/

 

public class ReportTotalsBySpecies extends HttpServlet {

public void service(

HttpServletRequest request,

HttpServletResponse response

) throws IOException, ServletException

{

ServletContext context = this.getServletConfig().getServletContext();

 

response.setContentType("text/html");

PrintWriter out = response.getWriter();

 

try

{

File reportFile = new File(context.getRealPath("trythis.jasper"));

 

JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

 

System.out.println(reportFile.getPath());

 

Map parameters = new HashMap();

//parameters.put("ReportTitle", "Address Report");

parameters.put("BaseDir", reportFile.getParentFile());

 

String jdbcDriver = "oracle.jdbc.driver.OracleDriver";

String databaseUrl = "jdbc:oracle:thin://@sablefish:1521:ifdb";

String userid = "ifdb";

String password = "ttspy2k";

Connection con = null;

try {

Class.forName(jdbcDriver);

System.out.println("JDBC Driver Loaded");

}

catch (Exception e) {

System.out.println("Failed to load JDBC Driver: " + e.getMessage());

return;

}

try {

con = DriverManager.getConnection(databaseUrl, userid, password);

System.out.println("Connection established with " + databaseUrl);

} catch ( Exception e ) {

System.out.println(String.valueOf("Error: " + e.getMessage()));

}

 

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);

 

JRHtmlExporter exporter = new JRHtmlExporter();

 

Map imagesMap = new HashMap();

request.getSession().setAttribute("IMAGES_MAP", imagesMap);

 

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);

exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);

exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");

 

exporter.exportReport();

}

catch (JRException e)

{

out.println("<html>");

out.println("<head>");

out.println("<title>JasperReports - Web Application Sample</title>");

out.println("<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">");

out.println("</head>");

 

out.println("<body bgcolor="white">");

 

out.println("<span class="bnew">JasperReports encountered this error :</span>");

out.println("<pre>");

 

e.printStackTrace(out);

 

out.println("</pre>");

 

out.println("</body>");

out.println("</html>");

}

}

}

 

 

 

 

 

 

By: Teodor Danciu - teodord

RE: Images not working in Webapp

2005-02-16 10:02

 

Hi,

 

You can put the IS_USING_IMAGES_TO_ALIGN

exporter parameter to Boolean.FALSE.

 

Have you registered the servlet that delivers the

report images? It is part of the "webapp" sample.

 

I hope this helps.

Teodor

 

 

 

 

 

 

By: Philippe Damerval - damerval

RE: Images not working in Webapp

2005-02-16 10:42

 

Thank you. What was puzzling me was that my package was in the same Tomcat installation as the webapp sample, the image url's (image?image=) looked the same and yet mine did not work. The problem was that I needed to register my servlet *mapping* in the web.xml as being part of the same package as the image servlet.

 

QUESTION: When designing and previewing reports with iReport in HTML format, a special folder is created for the images in the output folder with the pattern name htmlfilename_files. Is this purely an iReport feature, or is there a way to do this using jasper? It seems a more efficient technique than passing the images in the session if there are a lot of images.

 

To return to the resolution of the missing images issue, the image servlet and all other sample webapp servlets were registered in this form:

 

<servlet>

<servlet-name>ImageServlet</servlet-name>

<servlet-class>servlets.ImageServlet</servlet-class>

</servlet>

 

followed up with the mapping:

 

<servlet-mapping>

<servlet-name>ImageServlet</servlet-name>

<url-pattern>/servlets/image</url-pattern>

</servlet-mapping>

 

Once I mapped my servlet in this fashion: (with the common /servlets/ mapped 'package'):

 

<servlet-mapping>

<servlet-name>ReportTotalsBySpecies</servlet-name>

<url-pattern>/servlets/TotBySpecies</url-pattern>

</servlet-mapping>

 

The images worked fine. I suppose copying the ImageServlet to my own servlet package folder would have had the same effect.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...