JAva開発呼び出しiReport生成Htmlレポート


1 . Javaプロジェクトにフォルダを新規作成し、iReportで作成したレポートをフォルダの下にコピーし、プロジェクトに対応するJARパッケージを追加します.
 2. SpringBeanを作成します.java:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class SpringBeanUtil implements ApplicationContextAware  {

    private static ApplicationContext applicationContext = null;


    /**
     *  ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     *  applicationContext Bean,  .
     */
    public static Object getBean(String name) {
        return  applicationContext.getBean(name);
    }

    

    /**
     *  SpringContextHolder ApplicationContext Null.
     */
    public static void clearHolder() {
        applicationContext = null;
    }

    /**
     *  ApplicationContextAware ,  Context .
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
      //  SpringContextHolder.applicationContext = applicationContext; 
        this.applicationContext = applicationContext;
    }

  
 
}

3.ReportServiceletを作成する.java:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataSource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperRunManager;
public class MReportServlet extends HttpServlet {
	/**
	 *  HTML 
	 *
	 */
	private static final long serialVersionUID=1L;

	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
//		DataSource dataSource = (DataSource) SpringBeanUtil.getBean("dataSource");
		try {
			response.setContentType("text/html;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			
			PrintWriter printWriter = response.getWriter();
			java.io.File file = null;
			// iReport 
			if(request.getParameter("rpt").equals("jhw")){
				 file = new java.io.File(this.getServletConfig().getServletContext().getRealPath("/iReport/InventoryOverflowForInOut.jasper"));
			}
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Map map = new HashMap();
			Object object = request.getParameter("ticket_id");
			map.put("OVERFLOW_TICKET_ID",object.toString());
			java.sql.Connection connection =  DriverManager.getConnection("jdbc:oracle:thin:@192.168.70.31:1521:orcl","dms_dev","dms_dev");
			JasperRunManager.runReportToHtmlFile(file.getPath(), map, connection);
			response.sendRedirect("iReport/InventoryOverflowForInOut.html?id="+object);
			printWriter.flush();
			printWriter.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	
}

4.Webを作成する.xml:
  
    ireport  
    com.ilogst.ireport.MReportServlet  
    
    
    ireport  
    /ireport  
   

5.プロジェクトを起動し、URlから渡された値に基づいてif elseで異なるレポートを呼び出す
http://localhost:8080/iReport?rpt=jhw&ticket_id=PO201704170001