Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to open my Ireport in new browser tab window

user451648May 19 2013 — edited May 19 2013
hi
I'm using Jdev v 11.1.2.3.0
Isucced in conneting and calling (ireport) generated report from my adf application in pdf file format

All what I want to do now is to call or open the report in browser tab window

I can call it in the same tab window

using this line code
response.setHeader("Cache-Control", "max-age=0");
or

make attachment download
using this line code
response.setHeader("Content-Disposition", "attachment; filename=\"report.pdf\"");

please help me in this last setp


here is my code to call ireport
after adding nessesary jar files and Datasource in wedlojic
maybe it help others


============

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import java.sql.Connection;

import java.util.HashMap;
import java.util.Map;

import javax.faces.context.FacesContext;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.type.WhenNoDataTypeEnum;
import net.sf.jasperreports.engine.util.JRLoader;

import oracle.adf.model.BindingContext;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.BindingContainer;


//import oracle.adf.model.binding.DCIteratorBinding;


public class mainNew {
private RichInputText copyFrom;
private RichInputText copyTo;

public mainNew() {
}

public void setCopyFrom(RichInputText copyFrom) {
this.copyFrom = copyFrom;
}

public RichInputText getCopyFrom() {
return copyFrom;
}

public void setCopyTo(RichInputText copyTo) {
this.copyTo = copyTo;
}

public RichInputText getCopyTo() {
return copyTo;
}

public String cb3_action() {
String s =(String)copyFrom.getValue();
String d =(String)copyTo.getValue();


// copyTo.setValue(s);

// DCIteratorBinding empIter = (DCIteratorBinding) getBindings().get("Employees1Iterator");
// String empId = empIter.getCurrentRow().getAttribute("DepartmentId").toString();


Map m = new HashMap();
System.out.println(s) ;
System.out.println(d) ;
// m.put("eployeeId", copyFrom.getValue() );

/*
if (s!=null) {
m.put("whr", "Where department_id ="+s); }


if (d!=null) {

m.put("whr", "Where employee_id= "+d);}
*/
if (s != null & d==null)
{ m.put("whr", "Where department_id ="+s);
}

if (s==null & d!=null) {
m.put("whr", "Where employee_id= "+d);}


if (s!=null & d!=null) {
m.put("whr", "Where department_id ="+s +" and employee_id="+d);}



if (s==null & d==null) {
m.put("whr", "Where 1=2");}


try
{
// runReport("empRep2.jasper", m);
runReport("empsDyn.jasper", m);

}
catch (Exception e)
{
}
return null;
}

public BindingContainer getBindings()
{
return BindingContext.getCurrent().getCurrentBindingsEntry();
}

public Connection getDataSourceConnection(String dataSourceName)
throws Exception
{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dataSourceName);
return ds.getConnection();
}

private Connection getConnection() throws Exception
{
return getDataSourceConnection("hrDS");
}

public ServletContext getContext()
{
return (ServletContext)getFacesContext().getExternalContext().getContext();
}
public HttpServletResponse getResponse()
{
return (HttpServletResponse)getFacesContext().getExternalContext().getResponse();
}
public static FacesContext getFacesContext()
{
return FacesContext.getCurrentInstance();
}
public void runReport(String repPath, java.util.Map param) throws Exception
{
Connection conn = null;
try
{
HttpServletResponse response = getResponse();
ServletOutputStream out = response.getOutputStream();
/// response.setHeader("Cache-Control", "max-age=0"); // opens in same page
response.setHeader("Content-Disposition", "attachment; filename=\"report.pdf\""); // genreat adownload file

// response.setHeader("Content-Disposition", "inline; filename=\"" + "Report.pdf\"");//opens in same page
response.setContentType("application/pdf");
ServletContext context = getContext();
InputStream fs = context.getResourceAsStream("/reports/" + repPath);
JasperReport template = (JasperReport) JRLoader.loadObject(fs);
template.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
conn = getConnection();
JasperPrint print = JasperFillManager.fillReport(template, param, conn);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(print, baos);
out.write(baos.toByteArray());
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
}
catch (Exception jex)
{
jex.printStackTrace();
}
finally
{
close(conn);
}
}

public void close(Connection con)
{
if (con != null)
{
try
{
con.close();
}
catch (Exception e)
{
}
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2013
Added on May 19 2013
3 comments
2,236 views