Skip to main content

Posts

Showing posts from 2012

Reading and Writing Properties file in Java

Reading and Writing Properties file Java import java.io.FileInputStream; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; public class App {     @SuppressWarnings("rawtypes") public static void main( String[] args )     {           Properties prop = new Properties();         try {          // Loading Property file          // This method to load file in Simple Java Application.                 prop.load(new FileInputStream("messages.properties"));                                  // This method to load file when developing web application                 //prop.load(this.getClass().getClassLoader().getResourceAsStream("messages.properties"));                                  // Start ::                 //Writing Content to Property file                 prop.setProperty("ID121", "Angad Yadav");                 prop.setProperty("ID122", "Mani

HelloWorld Sample Application in Spring 3.0

Step 1: Open your Eclipse IDE, create a new Project under Dynamic Web Project Step 2: Download the jar file 1. commons-io-1.2.jar 2. commons-logging-1.1.1.jar 3. jstl-1.2.jar 4. log4j-1.2.14.jar 5. servlet-2.3.jar 6. slf4j-api-1.5.6.jar 7. slf4j-log4j12-1.5.6.jar 8. spring-asm-3.0.3.RELEASE.jar 9. spring-beans-3.0.3.RELEASE.jar 10.spring-context-3.0.3.RELEASE.jar 11.spring-core-3.0.3.RELEASE.jar 12.spring-expression-3.0.3.RELEASE.jar 13.spring-web-3.0.3.RELEASE.jar 14.spring-webmvc-3.0.3.RELEASE.jar 15.validation-api-1.0.0.GA.jar Now create lib folder under WEB-INF folder and copy all the jar files inside lib folder Now add these jar file to the project. Step 3: 1. Create index.jsp inside WebContent folder. 2. Create hello.jsp inside WEB-INF/views/ folder. 3. Create dispatcher-servlet.xml under WEB-INF folder. 4. Create package name controller and create class HelloWorldController.java inside package controller. for more detail refer below figure.

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.

Numeric Validation in Javascript

Numeric Validation Method function onlyNumbers(evt) {     var e = event || evt; // for trans-browser compatibility     var charCode = e.which || e.keyCode;     if (charCode > 31 && (charCode < 48 || charCode > 57))         return false;     return true; } Character Validation Method function onlyAlpha() {   var keyCode = window.event.keyCode;    if ((keyCode < 65 || keyCode > 90) &&     (keyCode < 97 || keyCode > 123) && keyCode != 32)      return false;    return true; } *****Output****** Allow Only Numbers Accept only Number : Accept only Character :

JSON parsing in JAVA

JSON parsing in JAVA package jsonsample; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class JsonSample { public static void main(String[] args) { String str = "{'status': 'ok','found': true," + "'header': {'queried_sn': 'T0650GW872832', 'mfpro_env': 'deere_staging', 'elapsed_seconds': 2.314,'timestamp': '2012-06-14 07:12:18'}," + "'machine': { 'mfpro_machine': {'updated_at': '2012-05-22T10:23:54-04:00','matches': 3,'hours': 6809,'id': 1140530,'features': " + "[{'key': 'pat','label': 'Power-Angle-Tilt','feature_key': 'cons_blades'}]}," + "'make': {'name': 'John Deere','id': 100,'pi': null}," + "

Dynamically adding content in Dropdown in Javascript

<html> <head> <script type="text/javascript"> function addOption(dropdown,text,value ) { var optn = document.createElement("OPTION"); optn.text = text; optn.value = value; dropdown.options.add(optn); } </script> </head> <select id="year" name="year" style="width: 70px" ></select> <script type="text/javascript"> var date = new Date(); var startYear = '2000' ; var currentYear = date.getFullYear(); do   {    addOption(document.getElementById("year"), currentYear, currentYear);    currentYear--;   } while (currentYear >= startYear); </script> </BODY> </html> *************Output*************

C# List Examples

C# List Examples 1. Adding values ~~~ Program that adds elements to List (C#) ~~~ using System.Collections.Generic; class Program {       static void Main()       {             List list = new List();             list.Add(2);             list.Add(3);             list.Add(5);             list.Add(7);       } } *********************************************************************************** 2. Adding objects. Using Add method ~~~ Program that uses Add method (C#) ~~~ using System.Collections.Generic; class Program {       static void Main()       {             // Add first four numbers to the List.             List primes = new List();             primes.Add(2);             primes.Add(3);             primes.Add(5);             primes.Add(7);       } } *********************************************************************************** 3. Adding objects -- Program that adds objects to List (C#) --- us

How to Stop Thread in Java

  Methods for Stopping a Thread     private volatile Thread thrd; public void stop() { thrd = null; } public void run() { Thread thisThread = Thread.currentThread(); while (thrd == thisThread) { try { thisThread.sleep(interval); } catch (InterruptedException e){ } repaint(); } }   The volatile keyword is used to ensure prompt communication between threads.    A field may be declared as volatile , in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.”

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } 4. Collections in Java? Interfaces Iterable Collection Generic Collections List Set SortedSet N

Toggle Event in Javascript

Toggle Event in Javascript This functions describes how the link toggles from On state to Off state. <script type="text/javascript"> function toggle_visibility(event) {              event = event || window.event;              var target = event.target || event.srcElement;        var id = target.id;        if(id == 'on')        {         document.getElementById("on").style.display = 'none';         document.getElementById("off").style.display = 'block';        }        else if(id == 'off')        {         document.getElementById("on").style.display = 'block';         document.getElementById("off").style.display = 'none';        }     } </script> <body> Copy & Paste the below code inside html page to get toggle effect <a href="#" id="on" onclick="toggle_visibility(event)" style="d