Tuesday, January 3, 2017

Cross domain security error dwr

You can avoid security checking in client side in dwr by adding following code into web.xml file

<servlet>
<servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
           <param-name>debug</param-name>
           <param-value>false</param-value>
        </init-param>
        <init-param>
           <param-name>accessLogLevel</param-name>
          <param-value>EXCEPTION</param-value>
        </init-param>
        <init-param>
          <param-name>crossDomainSessionSecurity</param-name>
          <param-value>false</param-value>
        </init-param>
</servlet>

Thursday, December 29, 2016

Remove last char in vi

Following Linux commands can be used to replace last character in vi editor.

1. Open file using vi editor
2. Run the following command to replace last char

:%s/.$//g

3. Save file using :wq!

Sunday, December 18, 2016

Create soft link in Linux

Suppose you want to create shortcuts in Linux, you can use 'ln -s' command to create soft links.

Ex :- If you need to run JAVA6 and JAVA8 both from terminal, you have to add java6 and java8 commands to /usr/bin for terminal execution.

Steps to keep java6 and java8.

  1. Create two files in /usr/bin/ as java6 and java8
  2. Extract JAVA6 and JAVA8 to Linux file system. Suppose directory locations are /usr/java6/ and /usr/java8/
  3. Create Soft links for java6 and java8 using the following commands as root.

ln -s /usr/java8/jdk1.6.0_10/bin /usr/bin/java6

ln -s /usr/java8/jdk1.8.0_12/bin /usr/bin/java8

Now check versions using following commands in terminal.

java6 -version
java8 -version

Thursday, December 15, 2016

Timestamp format in MySQL

This inbuilt MySQL command can be used to cast timestamp values as required.

SELECT DATE_FORMAT('field_name', '%Y-%m-%d %H:%i:%s') FROM 'table_name'

Above SQL will format timestamp in "YYYY-MM-DD HH:MM:SS" format. If you require only date you can format timestamp as following SQL.

SELECT DATE_FORMAT('field_name', '%Y-%m-%d') FROM 'table_name'

Add number of days to timestamp in MySQL

Following DATE_ADD('field_name', INTERVAL) can be used to increase the number of days in the 'field_name' in MySQL databases. 

SELECT SQL

SELECT DATE_ADD('field_name', INTERVAL 2 DAY)FROM 'table_name';

Update SQL

UPDATE 'table_name'
SET field_name= DATE_ADD('field_name' , INTERVAL 2 DAY)
WHERE 'id' = 1;

Tuesday, November 29, 2016

Change enum values by passing values

Following code can be used to change the enum return value as required by passing parameters.

import java.text.MessageFormat;

public enum Errors {
     USERNAME_NOT_FOUND("User not found"),
     USERNAME_EXISTS("Username {0} already exists."),
     USERNAME_CONTAINS_INVALID_CHARS("Username {0} contains invalid characters {1}.");

     private final String message;

     Errors(String message) {
          this.message = message;
     }

     @Override
     public String toString() {
          return message;
     }

     public String getMessage(Object... args) {
          return MessageFormat.format(message, args);
     }
      
     public static void main(String args[]) {

System.out.println(Errors.USERNAME_NOT_FOUND);          System.out.println(Errors.USERNAME_EXISTS.getMessage("username"));  System.out.println(Errors.USERNAME_CONTAINS_INVALID_CHARS.getMessage("us%ername", "%"));

     }
}


Wednesday, November 2, 2016

Run different main classes from one project in JAVA

You can use following command to run different classes from one project jar file. Suppose Runner1 and Runner2 are main classes. You want to run both in separately, then you can use the following commands.

java -cp App.jar foo.Runner1
java -cp App.jar foo.Runner2