Monday, May 21, 2012

iCal4j example - Set a Meeting using iCalendar

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter propose another meeting date/time.

iCalendar is used and supported by a large number of products, including Google Calendar, Apple iCal,IBM Lotus Notes,Yahoo Calendar, Microsoft Outlook etc

iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. To know more about iCal4j, click here.

About the example

This example is straight forward, we will create a calendar (net.fortuna.ical4j.model.Calendar) object and add a Meeting event to it. You can set various properties to the event like subject, location, description etc. Obviously you can customize it according to your requirement. This example will generate a .ics file (TestCalendar.ics). You can send this generated file via email to interested parties. Please see the below self explanatory java program.

For running below example you will need to add below mentioned JAR files in your classpath.
  • ical4j-1.0.3.jar
  • backport-util-concurrent-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-lang-2.6.jar

To download the above JAR file, click here.

Friday, May 18, 2012

Text Extraction with Java


Documents in PDF format, are widely used and accepted within the computer world. And while it is a versatile piece of software, it does have its downsides. The most obvious being the inability to alter the document natively, therefore, text extraction is often sought after to try and make this tedious task less of an uphill struggle.

Extracting all of the useful, interesting, and relevant information from a PDF can be a daunting task, but by utilising text extraction software which is completely written in Java. The process of extraction and conversion has become increasingly crucial for the manipulation and archiving of data. With the use of a Java application, this has become extremely easy in regards to extracting text from a PDF document.


Java simple iCalendar iCal4j example

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. To know more about iCal4j, click here.

About the example

ICalendarExample.java will create a .ics file lets say mycalendar.ics. It will create a calender event, you can customize it according to your requirement. You can send this generated file via email.

For running below example you will need to add below mentioned JAR files in your classpath.
  • ical4j-1.0.3.jar
  • backport-util-concurrent-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-lang-2.6.jar

To download the above JAR file, click here.

Thursday, May 10, 2012

JSTL forEach loop to iterate List, Map, Map of List, List of Map

Lets see how we can use JSTL to iterate List, Map, Map of List and List of Map. Below are the tested JSP codes, directly you can run below JSPs and see the outputs.

Note : The JSTL tag library needs jstl-1.2.jar in application's classpath. To download jstl-1.2.jar, click here

1. List

Lets see how to iterate List using JSTL forEach loop. Using below code you can iterate ArrayList, Vector or LinkedList.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> list = new ArrayList<String>();
    list.add("val1");
    list.add("val2");
    list.add("val3");
    
    request.setAttribute("MyList", list);
%>
<html>
<body>
    <c:forEach items="${MyList}" var="listItem">
        ${listItem} <br/>
    </c:forEach>
</body>
</html>

The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

If you get below mentioned error

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:53)
 org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
 org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:116)
 org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:310)
 org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:152)
 org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:410)
 org.apache.jasper.compiler.Parser.parseDirective(Parser.java:475)
 org.apache.jasper.compiler.Parser.parseElements(Parser.java:1425)
 org.apache.jasper.compiler.Parser.parse(Parser.java:138)
 org.apache.jasper.compiler.ParserController.doParse(ParserController.java:239)
 org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
 org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:197)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:372)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Probably the reason could be you have used below mentioned taglib in your JSP file.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

You need to get the right version of JSTL in your application's classpath, they use different URIs. If that one's not working, try downloading JSTL 1.2 (jstl-1.2.jar) from http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar, or pull down the 1.1 JSTL jars.

To know more about JSTL Taglib, Please see below links.
http://docs.oracle.com/javaee/5/tutorial/doc/bnake.html
http://jakarta.apache.org/taglibs/doc/standard-doc/GettingStarted.html

Thursday, May 3, 2012

Java : Create xls or xlsx files using Apache's POI

Apache's POI-HSSF and POI-XSSF is a Java API to access Microsoft Excel Format Files.

HSSF is used to create Excel '97(-2007) file format and XSSF is used to create Excel 2007 OOXML (.xlsx) file format.

HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets. They provide:
  • low level structures for those with special needs
  • an eventmodel api for efficient read-only access
  • a full usermodel api for creating, reading and modifying XLS files
To know more, click here

To run below programs you will need to add POI jar file i.e. poi-bin-3.8-20120326.zip. To download latest version of jar files, click here

For running below programs i have used apache poi-3.8. First we will create .xls file then we will see how to create .xlsx file

Tuesday, May 1, 2012

Spring Hello World Example

For running this example we have used MyEclipse 8.6.1, to download Latest version of MyEclipse go to below URL

Step by step tutorial to implement Spring hello world example

Step 1. Create a New Java Project
New -> Java Project



java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:145)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:84)
        at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
        at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:58)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
        at TestHelloSpring.main(TestHelloSpring.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

If you get above error, probably your application is not able to find commons-logging.jar in classpath.

Download the latest version of JAR file from below URL and add it to classpath of your applications classpath

http://commons.apache.org/logging/download_logging.cgi

To know more about Apache Commons Logging, click here

Step by step guide to implement Hello world servlet

In this example we will see how to do coding, compiling, deploying, and running a simple Hello World servlet which will just print "Hello World" in the browser.

To learn life cycle of servlet, click here

We will useTomcat server to run our HelloWorld servlet. To download tomcat, click here

Lets create our project folder in webapps lets say TestServlet

Example:
C:\apache-tomcat-7.0.11\webapps\TestServlet

Now lets create our first hello world servlet, lets say HelloWorldServlet.java

HelloWorldServlet.java

C:\apache-tomcat-7.0.11\webapps\TestServlet\WEB-INF\classes\HelloWorldServlet.java
/* HelloWorldServlet.java */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) {
  doPost(request,response);
 }
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) {
  PrintWriter pw = null;
  try {
   pw = response.getWriter();
  } catch (IOException e) {
   e.printStackTrace();
  }
  pw.println("<html>");
  pw.println("<body>");
  pw.println("<h2>Hello World!!</h2>");
  pw.println("</body>");
  pw.println("</html>");
 }

}