report Posts

ServletContext.getRealPath Always Return Null on Apache Tomcat 8

I met a very strange error, that somehow works on Tomcat 7, but not working on Tomcat 8. I’m using ServletContext’s class getRealPath method, for getting my application’s absolute file path. Here is my code,

@PostConstruct
    private void init() {
        try {
            // get report location
            String reportLocation = servletContext.getRealPath("WEB-INF");           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

On this code, i’m trying to get my report’s location and then processing it, by using servletContext.getRealPath. Which works well on my Apache Tomcat 7, but not well enough on Apache Tomcat 8. Somehow it always show null instead of the absolute path of my “WEB-INF” folder.

The workaround is actually quite simple, adding slash in front of “WEB-INF” make the problem disappear.

@PostConstruct
    private void init() {
        try {
            // get report location
            String reportLocation = servletContext.getRealPath("/WEB-INF");           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
Google+

[Java] Creating a Microsoft Word Report Easily using Servlet and A Plain HTML Template

Yesterday one of my friend, Edward Barchia, asked me a very simple question, “how can i creating an MSWord Report without using Jasper Report”. Because somehow, jasper report creates a very weird output when exported into Word file (.doc).

One fastest solution is using a html file, and renamed into .doc. Not an elegant solution, but perhaps (at that moment) is the best solution we had.

Okay, so basically what i need is only 2 files, 1 html template and a java servlet files. This is my HTML template, i put it on drie E and name it hello.html

<html>
	<head></head>
	<body>
		Test, hello ${name}
	</body>
</html>

And this is my servlet file,

package com.edw.testdownloaddoc.servlet;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;

@WebServlet(name = "downloadServlet", urlPatterns = {"/downloadServlet"})
public class DownloadServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedReader downloadFile = new BufferedReader(new FileReader("e:\\hello.html"));
        FileInputStream inputStream = null;
        OutputStream outStream = null;
        try {
            String s;
            StringBuilder sb = new StringBuilder();
            while ((s=downloadFile.readLine()) != null) {
                if(s.contains("${name}"))
                    s = s.replace("${name}", "Edwin");
                sb.append(s);
            }
 
            response.setContentLength((int) sb.toString().length());
            response.setContentType("application/msword");           
 
            // response header
            String nameFile = "hello.doc";
            String headerKey = "Content-Disposition";
            String headerValue = String.format("attachment; filename=\"%s\"",nameFile);
            response.setHeader(headerKey, headerValue);
 
            // Write response
            outStream = response.getOutputStream();
            IOUtils.write(sb.toString(), outStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            IOUtils.closeQuietly(downloadFile);
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outStream);
        }
    }

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

Oh, and before i forgot, this is my pom.xml dependencies

<dependencies>
	<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.2</version>
	</dependency>
	<dependency>
		<groupId>javax</groupId>
		<artifactId>javaee-web-api</artifactId>
		<version>7.0</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

This is the final output looks like,
helloworld

Google+

Fixing Jasper Report Error, “Font ‘Arial’ is not available to the JVM.”

Yesterday, i had a weird error when deploying to my Red Hat machine. Somehow my pdf generated report is throwing exceptions, it said “Font ‘Arial’ is not available to the JVM.”. A little bit weird since on my development machine, everything is running well.

After i run this command,

fc-list | grep "Arial"

I found out that “Arial” font isnt installed on my RedHat server, a little bit tricky due to im unable to run “yum” command because the server is behind a network firewall. So my workaround is download a .rpm, and installed it manually. After several minutes googling, i found a very recommended .rpm installer.


http://www.mjmwired.net/resources/mjm-fedora-f11.html#ttf

I installed it using

 sudo rpm -ivh msttcore-fonts-2.0-3.noarch.rpm

And check my installed fonts

fc-list | grep "Arial"

Now i can see “Arial” fonts on my list of installed fonts on RedHat, restart my tomcat and my pdf reporting is running well again.

Here is the complete stacktrace of the error,

21:57:12,135 INFO  [stdout] (AsyncLogger-1) <2014-09-29 21:57:12,131>,[http-/0.0.0.0:80-94]>>[ERROR]Font 'Arial' is not available to the JVM. See the Javadoc for more details. net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Arial' is not available to the JVM. See the Javadoc for more details.
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fonts.FontUtil.checkAwtFont(FontUtil.java:357)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.loadFont(SimpleTextLineWrapper.java:369)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.getGeneralFontInfo(SimpleTextLineWrapper.java:339)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.createFontInfo(SimpleTextLineWrapper.java:279)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.start(SimpleTextLineWrapper.java:241)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.TextMeasurer.measure(TextMeasurer.java:537)
21:57:12,136 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:623)
21:57:12,136 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.JRFillTextField.prepare(JRFillTextField.java:699)
Google+

Create and Exporting A Web-Based Excel Report Using JXLS and SpringMVC

Today im trying to create a simple excel exporter report using SpringMVC. Im integrating JXLS library with SpringMVC framework. I prefer using JXLS compared to JasperReport or other java-to-excel-library due to its easy templating, so i dont need to create excel formatting from java code.

I start with my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edw</groupId>
    <artifactId>JXLSSpringMVC</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>JXLSSpringMVC</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency> 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        
        <!-- jxls -->
        <dependency>
            <groupId>net.sf.jxls</groupId>
            <artifactId>jxls-core</artifactId>
            <version>0.9.9</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

And a simple web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

And 2 SpringMVC configuration file, applicationContext.xml,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    
    <tx:annotation-driven/>
    
    <context:component-scan base-package="com.edw.jxlsspringmvc"/>
    
</beans>

and dispatcher-servlet.xml,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.edw.jxlsspringmvc.controller" />
	
    <mvc:annotation-driven />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
</beans>

And a simple excel file,
jxls excel template

A java controller file,

package com.edw.jxlsspringmvc.controller;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author edwin < edwinkun at gmail dot com >
 */
@Controller
public class IndexController {

    @Autowired
    private ServletContext context;
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }
    
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public String export(HttpServletRequest request, HttpServletResponse response) {
        try {
            // set output header
            ServletOutputStream os = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=\"myexcel.xls\"");
            
            String reportLocation = context.getRealPath("WEB-INF");

            Map beans = new HashMap();
            beans.put("name", "Edwin");
            beans.put("address", "Jakarta, Indonesia");
            XLSTransformer transformer = new XLSTransformer();

            Workbook workbook = transformer.transformXLS(new FileInputStream(reportLocation + "/myexcel.xls"), beans);
            workbook.write(os);
            os.flush();

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

And my index.jsp file

<html>
    <head>
        <title>Download Excel</title>
    </head>
    <body>
        <a href="${pageContext.request.contextPath}/export">download excel</a>
    </body>
</html>

My Netbeans project structure is like this,
jxls netbeans project

And this is the result when im clicking on the website’s download url,
jxls download result

Have fun with JXLS :)

Google+

[Java] Membuat Laporan Excel di Aplikasi Web Based

Excel adalah salah satu format output yang disukai oleh enduser, selain dari pdf. Karena di excel pengguna bisa melakukan perhitungan-perhitungan, modifikasi ataupun kustomisasi yang lebih bebas dibandingkan dari pdf.

Di tutorial ini, akan dibahas bagaimana membuat export laporan aplikasi web-based ke format xls. Gw menggunakan library jxls (http://jxls.sourceforge.net/) karena lebih mudah dalam pembuatan format template excel jika dibandingkan dengan jasper. Konsepnya sebenarnya sederhana, sebuah servlet yang jika diinvoke dari method GET, akan meng-generate file excel dan mengirimkan ke pengguna langsung via browser.

Dimulai dengan membuat template excel yang akan kita isi dengan data (perhatikan baris ke-5), kemudian diletakkan dibawah folder WEB-INF.

Untuk sourcecodenya seperti biasa, sebuah java bean sederhana sebagai tempat untuk menampung data

package com.edw.bean;

public class Mahasiswa {

    private String nama;
    private String alamat;
    private int usia;

	// jangan lupa setter dan getter-nya    
}

Berikut adalah servlet yang digunakan untuk meng-generate laporan berformat xcel

package com.edw.servlet;

import com.edw.bean.Mahasiswa;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;

public class ReportServlet extends HttpServlet {

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

        // set output header
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=\"report." + new SimpleDateFormat("yyyyMMdd.hhmm").format(new Date()) + ".xls\"");

        // ambil file excelnya
        ServletContext context = getServletContext();
        String reportLocation = context.getRealPath("WEB-INF");

        // buat data
        List<Mahasiswa> mahasiswas = new ArrayList<Mahasiswa>();
        for (int i = 0; i < 10; i++) {
            Mahasiswa mahasiswa = new Mahasiswa();

            mahasiswa.setNama("Edwin " + new Random().nextInt());
            mahasiswa.setAlamat("Jakarta " + new Random().nextInt());
            mahasiswa.setUsia(new Random().nextInt());

            mahasiswas.add(mahasiswa);
        }

        // kirim ke excel
        Map beans = new HashMap();
        beans.put("x", mahasiswas);

        try {
            XLSTransformer transformer = new XLSTransformer();
            Workbook workbook = transformer.transformXLS(new FileInputStream(reportLocation + "/report.xls"), beans);
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }
}

Jangan lupa daftarkan servletnya di web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Reporting Excel</display-name>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>  
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
	
    <servlet>
        <servlet-name>reportServlet</servlet-name>
        <servlet-class>com.edw.servlet.ReportServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>reportServlet</servlet-name>
        <url-pattern>/reportServlet</url-pattern>
    </servlet-mapping>	  

</web-app>

Kemudian file jsp yang akan dijadikan sebagai halaman landing page

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Download</h1>
        <a href="reportServlet">here</a>        
    </body>
</html>

Jika dijalankan, akan menghasilkan aplikasi web seperti ini,

Ini adalah isi file excel hasil download ketika dibuka

Berikut adalah konfigurasi dan library yang gw gunakan di Netbeans gw,

Ga terlalu sulit kan bikin laporan pakek JXLS ;)

Google+