April 21, 2015

To call spring controller from ajax jquery in java spring

Ajax Jquery call to java spring controller


Description : The program will helps you to call java spring controller  from ajax jquery. To know spring configuration please refer this link. The link shows a  Hello world Model View Controller java spring example.

index.jsp


jquery api needed

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jqueryApps.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

These jquery api should be paste in your WEB-INF folder.


jqueryApps.js


function callJavaSpringController(){
var jqueryData = 'java spring mvc';
alert(senderEmail);

 $.ajax({
       type: "GET",
       url: "callcontroller.html",
       data: { message : jqueryData }
     }).done(function( msg ) {
       alert( "Data Saved: " + msg );
     });
}


JavaSpringSampleController.java


/**
JavaSpringSampleController  class
*/
package net.cfed.oms.controller;

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

import net.cfed.oms.model.SendMailModel;
import net.cfed.oms.service.EmployeeService;
import net.cfed.oms.serviceImpl.EmployeeServiceImpl;

import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author free source codes
 *
 */
@Controller
public class JavaSpringSampleController {

@RequestMapping("/callcontroller")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) {

String message = "Welcome  to";
                message = message + request.getParameter("message");
System.out.println(message);
return new ModelAndView("index", "message", message);
}
}


Here is a sample for ajax jquery call to servlet , click on the link

http://javabelazy.blogspot.in/

April 14, 2015

Automatically typing notepad in java


A Notepad that works automatically in java


/**
 * Funny notepad automatically wishing birthday
 */
package com.blogspot.javabelazy.funny;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author +Vipin Cp
 *
 */
public class HappyBirthdayMessage {

/**
* @param tokyo
*/
public static void main(String[] tokyo) {
// TODO Auto-generated method stub
HappyBirthdayMessage message = new HappyBirthdayMessage();
String yourMessage = "Happy Birthday Jerin V George";
Runtime runtime = Runtime.getRuntime();
try {
                       Thread.sleep(5000);
runtime.exec("notepad");
Thread.sleep(3000);
message.createMessage(yourMessage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void createMessage(String message) {
// TODO Auto-generated method stub
try {
Robot robot = new Robot();
int length = message.length();
for(int i=0;i<length;i++){
char letter = message.charAt(i);
String keyVal = Character.toString(letter);
String variableName;
if(keyVal.equals(" ")){
variableName ="VK_SPACE";
}else{
variableName = "VK_"+keyVal.toUpperCase();
}


Class clazz = KeyEvent.class;
   Field field = clazz.getField(variableName);
   int keyCode = field.getInt(null);
   robot.keyPress(KeyEvent.VK_SHIFT);
   robot.keyPress(keyCode);
   robot.delay(1000);
}

} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Java tips and trick by +belazy

http://javabelazy.blogspot.in/

April 13, 2015

Binary Search Algorithm implementation in java

Binary Search Algorithm in Java source code


Description

Find the position of specific input value within a sorted array (either descending/ascending).
The algorithm compares the key value with the middle value of array, if the key matches it will return the value or other wise returns -1 value.

Time complexity for binary
Worst : O (log n)
Average : O (log n)


Binary Search Algorithm Gif image
Binary Search Algorithm Computation

int []values = {1,2,5,6,12,25,26,27,30}; // sorted array


/**
 * Binary Search algorithm or Half interval search algorithm implementation
 * Find the position of a specific value (key) from/within a sorted array
 */

package com.blogspot.javabelazy.logics;

/**
 * @author javabelazy
 *
 */
public class BinarySearch {

private static int attempt = 0;

private int findIndex(int[] values, int target) {
return binarySearch(values,target,0,values.length-1);
}

private int binarySearch(int[] values, int target, int start, int end) {
attempt = attempt +1;
if(start > end){
return -1;
}

int middle = (int) Math.floor((start+end)/2);
int value = values[middle];

if (value > target) { return binarySearch(values, target, start, middle-1); }
if (value < target) { return binarySearch(values, target, middle+1, end); }

return middle;
}

/**
* @param binsearchalgo string
*/
public static void main(String[] binsearchalgo) {
int []values = {1,2,5,6,12,25,26,27,30}; // sorted array
int target = 27; // value to be find (the key in binary search algorithm)
BinarySearch binarySearch = new BinarySearch();
int position = binarySearch.findIndex(values,target);
System.out.println(" Size of the array to search : "+values.length);
System.out.println(" Value to be found : "+target);
System.out.println(" Position of the value found : "+position);
System.out.println(" Attempt made in finding value : "+attempt);
System.out.println(" www.javabelazy.blogspot.in ");

}
}

Output



BINARY SEARCH ALGORITHM IMPLEMENTATION IN JAVA
binary search algorithm implementation in java


Author : +belazy


Java source code for binary search algorithm



http://javabelazy.blogspot.in/

April 08, 2015

Fibonacci series implementation in java

How to find fibonacci series of a given number source code

package com.blogspot.javabelazy.programs;
import java.util.Scanner;
/*
 * Fibinocci number series implementaion in java
 * Series : 0, 1, 1, 2, 3, 5, 8, 13
 */


/**
 * @author vipin
 * Fibinocci Series Java full source code download
 */
public class FibinocciSeries {

/**
* @param args
*/
public static void main(String[] bestJavaBlog) {

System.out.println(" Enter the nth number to find the fibinocci value ");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();  // user input
//int input = 99;
int previousNumber = 0;
int number = 1;
int nextNumber = 0;
System.out.println("\n Fibinocci series upto "+input +"th number ");
for(int i=0;i<input;i++){
if(i==0){
//initialize
System.out.print(" "+previousNumber);

}else if(i==1){
System.out.print(", "+number);
}
else{
nextNumber = previousNumber + number;
System.out.print(", "+nextNumber);
previousNumber = number;
number = nextNumber;
}
}
System.out.println("\n Created by http://www.javabelazy.blogspot.in ");
}
}

Output

Fibonacci series source code
Fibonacci Series implementation in java



Fibonacci Series Algorithm


1. Start
2. Decalare variables incr, first, second, value
3. Initialize the variables first  as 0, value  as 0 and second as 1
4. Enter the nth term of the Fibonacci to be outputted
5. Print first two terms
6. Then loop the below code upto the nth term
    value = first + second
    first = second
    second = value
    increment variable incr each time by 1
7. Print the value in the variable value
8. Stop


About Fibonacci Sequence

The Fibonacci Series is a series of number where the next number is found by adding up last two number in the series.

Series : 0,1,1,2,3,5,8,13,21,34 and so on

Fibonacci Number, their ration is very close to Golden ration "phi" which is equal to 1.618.

Real world example : Fibonacci considers the growth of shell of snail, Petals of flower




Author : +ITSECTION CIVILSUPPLIES 

http://javabelazy.blogspot.in/

Facebook comments