Posts

Implementing Custom ResultType in Struts2

Image
there are different types of result types for a struts2 action. for full list please visit. http://struts.apache.org/release/2.1.x/docs/result-types.html from the book  how to implement a custom result type please see on   http://lkreddy.blogspot.in/2012/04/how-to-write-our-own-result-type-in.html public class my customresultType extends PlainTextResult{ } then in struts.xml file define this result type     and then use this in any action

Is Request Ajax: how to find

this is good way to find out if something is ajax or not in java public static boolean isAjax(request) { return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")); }

Simple JDBC tutorial

Simple JDBC tutorial .  load jdbc driver  Class.forName("oracle.jdbc.OracleDriver"); create a connection to database  Connection conn = java.sql.DriverManager.getConnection(url, username, password); url is jdbc:oracle:thin:@localhost:1521:orcl create statement Statement  stmt = this.conn.createStatement(); execute query stmt.executeUpdate(query); iterate over ResultSet

Loggers in JAVA

This tip shows how to use Logger in any java application. Logger needs to configure Formatter and Handler. There are many types of handlers and formatters present . In this example FileHandler is used to store all the log messages in a log file. And Simple formatter is used to format the log messages in human readable form. package  MyProject; import  java.io.IOException; import  java.util.logging.FileHandler; import  java.util.logging.Level; import  java.util.logging.Logger; import  java.util.logging.SimpleFormatter; public  class   MyLogger  {    public static  void  main ( String []  args ) {      Logger logger = Logger.getLogger ( "MyLog" ) ;      FileHandler fh;      try  {        // This block configure the logger with handler and formatter        fh =  new  FileHandler ( "c:\\MyLogFile.log" ,  true ) ;        logger.addHandler ( fh ) ;        logger.setLevel ( Level.ALL ) ;        SimpleFormatter formatter =  new  SimpleFormatter () ;        fh.setFormatter

Strings in Java

Image
To create string there are two ways 1. String s3= new String("hello");     String s4= new String ("hello"); 2. String s1= "hello";     String s2="hello"; which method is better ?  second method Because the content is same s1 and s2 refer to the same object where as s3 and s4 do not refer to the same object. The 'new' key word creates new objects for s3 and s4 which is expensive. StringBuffer and StringBuilder StringBuffer is used to store character strings that will be changed ( String objects cannot be changed). It automatically expands as needed. Related classes: String, CharSequence. it is synchronized . StringBuilder was added in Java 5. It is identical in all respects to StringBuffer except that it is not synchronized , which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringB