Monday, March 2, 2015

Java Thread's yield() method

Thread's yield() method

It is a static method. yield() method takes the currently running thread to back to the runnable state
so the other equal's priority thread will get a chance to run. but it is not guaranteed that
the same thread will not get in execution back. it can come back in the running state.

yield() method doesn't take the thread into wait/sleep/blocking state.
it just takes the current running thread into runnable state. just for giving chance to other threads in
the Runnable pool.


Keep Visiting 
Give your comment 

JAVA THREAD PRIORITY

A thread gets a default priority that is the priority of the thread of execution that creates it.

public class MainThread {
public static void main (String [] box) {
Thread th = new Thread();
}
}
the thread referenced by th will have the same priority as the main thread.

One can also set priority using the .setPriority() method.

The thread priority value must be in range 1 to 10.

Thread priority range is also depends on the JVM some JVM takes only values between 1 to 5.

JVM never changes the thread priority.

Any thread's default priority is 5.

There are three constants in the Thread class for the thread priority.
Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)

If user sets  1o 10 priorities for ten different threads and if the JVM doesnt support 1 to 10 priorities but 
it support 1 to 5 priority then some of the thread among the 10 will have duplicate priorities.


Saturday, February 28, 2015

STRUS2 Custom Validation example

Note: Put all  xml and java files into your src folder

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <action name="loginAction" class="LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="input">Login.jsp</result>
        </action>
    </package>
</struts>


 
validator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">

<validators>
    <validator name="alphabetsOnly" class="EliteValidator" />
    <validator name="digitsOnly" class="EliteValidator" />
</validators>


Login-Action.validation.xml

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
   
<validators>
   
    <field name="username">
       
        <field-validator type="requiredstring">
            <message>Empty Username !</message>            
        </field-validator>
       
       
        <field-validator type="alphabetsOnly">
            <message>Digits not allowed in Username</message>            
        </field-validator>
       
    </field>
   
    <field name="password">
       
        <field-validator type="requiredstring">
            <message>Empty Password !</message>            
        </field-validator>
       
    </field>
   
    <field name="mobileNumber">
       
        <field-validator type="requiredstring">
            <message>Empty Mobile Number !</message>            
        </field-validator>
               
        <field-validator type="digitsOnly">           
            <message>Alphabets not allowed in Mobile Number</message>            
        </field-validator>
       
    </field>
   
</validators>

LoginAction.java


import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
   
    private String username;
    private String password;
    private String mobileNumber;
   
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * @return the mobileNumber
     */
    public String getMobileNumber() {
        return mobileNumber;
    }
    /**
     * @param mobileNumber the mobileNumber to set
     */
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

}


EliteValidator.java


import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class EliteValidator extends FieldValidatorSupport{

    @Override
    public void validate(Object object) throws ValidationException {
            System.out.println("INSIDE VALIDATE OF ELITEVALIDATOR");
           
            String fieldName = getFieldName();
            String fieldValue = (String)getFieldValue(fieldName, object);
           
            System.out.println("FieldName: "+fieldName);
            System.out.println("fieldValue: "+fieldValue);
           
            if(fieldName.equalsIgnoreCase("username") && fieldValue.matches(".*\\d.*")){
                System.out.println("Found numbers in username");
                addFieldError(fieldName, object);
               
            } else if(fieldName.equalsIgnoreCase("mobileNumber") && (fieldValue.matches(".*\\d.*")==false)){
                System.out.println("Found Alphabets in mobileNumber");
                addFieldError(fieldName, object);
            }
           
            return;
    }

}


Note: PUT JSP IN THE WEBCONTEXT FOLDER OF YOUR APP
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<style>
    .errorMessage{
        color:red;
        font-size: 11px;
        text-align:right;
    }
</style>
</head>
<body>
    <center>
        <s:form action="loginAction" method="post" validate="true" >
            <s:textfield     name="username" label="Username"    />
            <s:textfield     name="password" label="Password"    />
            <s:textfield      name="mobileNumber" label="Mobile Number"/>
            <s:submit/>
        </s:form>
    </center>
</body>
</html>


Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    WELCOME TO CUSTOM VALIDATION DEMO PROJECT
</body>
</html>

Friday, February 20, 2015

JQUERY TO COUNT TOTAL SELECTED CHECKBOXES

function checkTotalSeleted(){

   var lengthTotal = $('input[name=ids]:checked').length;

    if(lengthTotal!="0"){
$("#totalSelected").html("Total selected: +lengthTotal);

    }else{
    $("#totalSelected").html("");
   }
}

Thursday, February 12, 2015

CHECK CHECKBOX AND CHANGE ROW COLOR, CLICK ROW AND CHECK CHECKBOX AND CHANGE ROW COLOR, CLICK PARENT AND CHECK ALL CHECKBOXES



$(document).ready(function() {

             //  CHECK PARENT CHECKBOX AND SELECT ALL CHILD CHECKBOXES

   $('#selectAll').click(function(event) {  //on click    
       if(this.checked) { // check select status
        $("[name=ids]").prop("checked", true); 
        $('tr').addClass('selected');  
       }else{
        $("[name=ids]").prop("checked", false);
        $('tr').removeClass('selected');
       }
   });

             //  SELECT CHECKBOX AND CHANGE ROW COLOR
   

   $("table").on("change", ":checkbox", function() {
    if(this.checked){
    $(this).parents("tr").addClass('selected');
    }else{
    $(this).parents("tr").removeClass('selected');
    }
   });

           // CLICK ON ROW AND CHANGE ITS COLOR AND CHECKED CHECKBOX
           $('#'+tableId ).on( 'click', 'tr', function (event) {
                  if (event.target.type !== 'checkbox') {
                  $(this).toggleClass('selected');
                         $(':checkbox', this).trigger('click');
                  }       

           });

});

Wednesday, February 11, 2015

JAVA SCRIPT HOW TO ADD FUNCTION INTO JSON OBJECT

var beanPropertyArray = [];

columnData = {
    "1": "One", 
     "2": "Two", 
    "myFun": function ( ) {
                                            return 1*2;
                                 }

beanPropertyArray.push(columnData);

Noyr : now direct use the beanPropertyArray as a JSON object it will give you the function in the json data. Do NOT use JSON.stringify(beanPropertyArray).


Tuesday, February 3, 2015

SLF4J DBSPY TEST CODE

Required Libs

log4j-1.2.16.jar
log4jdbc4-1.2.jar
log4j-over-slf4j-1.7.5.jar
slf4j-api.jar
slf4j-jdk14.jar
slf4j-log4j12-1.7.5.jar

slf4j-simple-1.7.5.jar

Code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class SpyEngine {

public static void main(String[] rk) {

try {

  Class.forName("net.sf.log4jdbc.DriverSpy");
Connection conn = DriverManager.getConnection( "jdbc:log4jdbc:oracle:thin:@192.168.8.87:1521:aaadb", "username", "password");

PreparedStatement prStmt = conn.prepareStatement("select * from tablename");
prStmt.execute();
prStmt.close();

  conn.close();
 
} catch (Exception e) {
System.out.println("Error: " + e);
} 
}

}

Keep Visiting..
Write your comment..


Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...