Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Wednesday, August 15, 2018

Read/Write large excel (xlsx) file

In this article you will see how to write a huge excel file.

Using SXSSFWorkbook you can write large excel file. It is a Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time.

Note - I have tested it to write 10,48,576 rows which created 40 MB excel file. If you want to write more rows than this then you may opt to write CSV file.

You need to add below dependencies in your pom.xml

Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAXB marshalling and unmarshalling of ArrayList

Marshalling and unmarshalling of ArrayList using JAXB is rather simple however you cannot directly marshall ArrayList. You will need a holder class (wrapper class). It will hold the arraylist that you want to marshall or unmarshall.

Please see the self explanatory java code below -

Output -

--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
    <Value>one</Value>
    <Value>two</Value>
    <Value>three</Value>
</List>

--Unmarshalling--

[one, two, three]

Wednesday, July 23, 2014

Integration of DWR using DWRSpringServlet

DWR is a Easy Ajax for Java. To know more click here.

In this article we will see sample codes for integration of DWR with DWRSpringServlet

Step 1 - Create a web project in eclipse.

Step 2 - Add below Spring libraries

  • Spring Core libraries
  • Spring Web libraries
  • Spring AOP libraries
Step 3 - Add dwr.jar
Download appropriate version of dwr.jar and add it in your application classpath. For example DWR 3 requires Spring version 2.5 or greater.

Step 4 - Create dependent files

web.xml
The DwrSpringServlet can be used if you are not using Spring MVC. This servlet gains access to the Spring context configured in your web.xml. You can create your own context file, in our case it is dwrcontext.xml.


dwrcontext.xml


DWRService.java


index.jsp


Step 5 - Deploy the project in tomcat (or any other web server) and check the output


Integration of DWR using Spring MVC

DWR is a Easy Ajax for Java. To know more click here.

In this article we will see sample codes for integration of DWR with spring MVC

Step 1 - Create a web project in eclipse.

Step 2 - Add below Spring libraries

  • Spring Core libraries
  • Spring Web libraries
  • Spring AOP libraries

Step 3 - Add dwr.jar
Download appropriate version of dwr.jar and add it in your application classpath. For example DWR 3 requires Spring version 2.5 or greater.

Step 4 - Create dependent files

web.xml
Create spring DispatcherServlet and map URL pattern as shown below

javaxp-servlet.xml
If you are using Spring MVC you must declare dwr controller and url mapping i.e. SimpleUrlHandlerMapping as shown below

DWRService.java

index.jsp

Step 5 - Deploy the project in tomcat (or any other web server) and check the output


Thursday, June 19, 2014

Java - Authenticate users in Active directory and using LDAP

First lets me try to explain you in brief what is Active directory and LDAP

Active Directory is a database based system that provides authentication, directory, policy, and other services in a Windows environment

LDAP (Lightweight Directory Access Protocol) is an application protocol for querying and modifying items in directory service providers like Active Directory, which supports a form of LDAP.

In short, Active Directory is a directory services database and LDAP is one of the protocols you can use to talk to it.

1. Authenticate users in Active Directory

Directly you can run this code, no need to add any jar file.



Friday, June 6, 2014

Java - To Load a property file

Property files are used to store static information in a key-value pair. Things that you do not want to hard code in your Java code, which tend to change in future goes into properties files. Also as it is a static information you do not want to load the property file for each and every request as it may hamper performance. You may need it to be loaded only once at the begining of the loading of application.

You can load properties file using below methods. You can provide absolute path as well as relative path however mostly relative path is preferred.

prop.load(AppProperties.class.getClassLoader().getResourceAsStream("app.properties"));
OR
prop.load(new FileInputStream(new File("C:/temp/app.properties")));

Please see the self explanatory java code below - 

Saturday, May 24, 2014

New Features of Java 7

Java 7 contains many new features, enhancements and bug fixes to improve efficiency to develop and run Java programs.

Here is a brief summary of the enhancements included with the Java 7 release:

  • Improved performance, stability and security.
  • Enhancements in the Java Plug-in for Rich Internet Applications development and deployment.
  • Java Programming language enhancements that enable developers with ease of writing and optimizing the Java code.
  • Enhancements in the Java Virtual machine to support Non-Java languages.

In this article, we will explore some of those capabilities through code samples.

1. Binary literals

You can use the binary number system (0s and 1s) to express integral types byte, short, int, and long. To specify a binary value, prefix the value with a 0b or 0B.


--------------------- Output ---------------------
a = -88
b = 112
c = 73
d = 1467

2. Underscore literals

To improve readability of numeric literals in the code, you can use underscores between digits to separate a group of digits. You can use as many underscores as you need.


--------------------- Output ---------------------
a = -86
b = 4554
c = 123456789
d = 111222333
e = 11.227799


Monday, April 14, 2014

Java program to create doc/docx file

Apache POI can be used to create a doc/docx file. To know more click here.

You can download latest version of jar files(i.e. poi-bin-3.10-FINAL-20140208.zip) form below link.
http://poi.apache.org/download.html

Add below mentioned Jar files in your classpath -

  • poi-3.10-FINAL-20140208.jar
  • poi-ooxml-3.10-FINAL-20140208.jar
  • poi-ooxml-schemas-3.10-FINAL-20140208.jar
  • poi-scratchpad-3.10-FINAL-20140208.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar

Please see the self explanatory java code below

Java program to read doc/docx file

Apache POI can be used to read doc/docx file. To know more click here.

You can download latest version of jar files(i.e. poi-bin-3.10-FINAL-20140208.zip) form below link.
http://poi.apache.org/download.html

Add below mentioned Jar files in your classpath -
  • poi-3.10-FINAL-20140208.jar
  • poi-ooxml-3.10-FINAL-20140208.jar
  • poi-ooxml-schemas-3.10-FINAL-20140208.jar
  • poi-scratchpad-3.10-FINAL-20140208.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar
Please see the self explanatory java code below


Saturday, February 22, 2014

Java : Encode/Decode Base64

We will be using Apache Commons codec to Encode and Decode Base64 data.

Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs. To know more, click here.

To run below example you will need to add commons-codec.jar in your classpath. Download commons-codec-1.4.jar.

Please see the self explantory java code below.

Friday, January 31, 2014

Implementation of Restful Webservice with image

In Java EE 6, JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions. Before proceeding please read this article, Developing RESTful Web Services with JAX-RS.

Also please have look at the tutorial about "JAXB marshalling and unmarshalling", as it would be helpful to understand Restful Webservice.

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.5
  • MyEclipse 8.6.1
1. Create a new Web Service Project, File -> New -> Web Service Project


Wednesday, January 29, 2014

Java program to download file from url

Below is the java program to download a file from url.


Java code to send meeting request using iCal4j

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.

Java program to send email with attachments

Note: To run below example u need to add JavaMail jar files in your classpath

Java program to read csv file

Below is the java program to read a csv file.

Lets assume we have a below csv file.

Country.csv

"Country","Capital","President"
"India","Delhi","Pranab Mukherjee"
"United Kingdom","London","David Cameron"
"China","Beijing","Hu Jintao"
"United States","Washington","Barack Obama"

CSVReader.java


Output

"Country" "Capital" "President"
"India" "Delhi" "Pranab Mukherjee"
"United Kingdom" "London" "David Cameron"
"China" "Beijing" "Hu Jintao"
"United States" "Washington" "Barack Obama" 

Wednesday, November 13, 2013

Java program to play an audio sound

These are the following ways in which you play a sound in Java.

Toolkit - If you just want a beep or quick alert you can use beep() finction of class java.awt.Toolkit. It Emits an audio beep.
AudioClip - The AudioClip interface is a simple abstraction for playing a sound clip.
Clip - The Java Sound API provides functionality for the capture, processing, and playback of sampled audio data & the sequencing and synthesis of MIDI data.
MIDI Sequences - The javax.sound.midi package provides an interfaces and classes for I/O, sequencing, and synthesis of MIDI (Musical Instrument Digital Interface) data.

Please see the sample code of each type


Wednesday, November 6, 2013

Example using Java.awt.Robot class

Java.awt.Robot class is used to take the control of mouse and keyboard. In this example we will open a notepad and type a message. Please see the self explanatory code below.

Monday, February 18, 2013

PHP example - Google Recaptcha validation using jquery (AJAX)

In earlier post we have seen JSP example, how to validate Recaptcha using jquery,  Now we will see how to achieve the same in PHP.

To know more about Google recaptcha, click here.

To run php example add recaptcha-php-1.11 in your application. Please see the self explanatory PHP code below.

test.php

JSP example - Google Recaptcha validation using jquery (AJAX)


Below is the example for Google Recaptcha validation using jquery(AJAX).

What is reCAPTCHA?

reCAPTCHA is a free CAPTCHA service that protects your site against spam, malicious registrations and other forms of attacks where computers try to disguise themselves as a human; a CAPTCHA is a Completely Automated Public Turing test to tell Computers and Human Apart. reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.

To know more about Google recaptcha, please see below links
http://www.google.com/recaptcha
https://developers.google.com/recaptcha/

Please see self explanatory JSP code below. To run below example add recaptcha4j-0.0.7.jar file in your applications classpath.

test.jsp