Friday, June 29, 2012

Another way of System.out.println();

Hi Guys,

                         This is the another way of printing something to console.

import static java.lang.System.out;
 
public class ShortSOP {
public static void main(String[] args) {
out.println("Hello, world");
}
}

Thanks,

Rajesh Kumar yuvaraj

Performance Trade offs of System.out.println();

 Hi Guys,


                     Do you know that the more number of System.out.println(); leads to the slowness of the project, Why, The method println() is a synchronized  which leads to slowness in terms of execution.


Thanks

Rajesh Kumar Yuvaraj

The "Best Ever " example for Synchronization !!!!!!!

Hi Guys,
          
              As you know that  we can discuss half of the J2SE using the statement which is executed most number of times in this Universe, that is

                    i.e: System.out.println("");

Using this statement we can explain the concept of Synchronization in this way.

  • System is a final class which can not be extended.
  • out is a static variable in side the System class of type PrintStream.
  • println() is a method if PrintStream class.
  • We have totally 10 println() methods inside of this PrintStream class.
  • each and every println() method is synchronized.
 i.e    public void println(String x) {
        synchronized (this) {
        print(x);
        newLine();
     }
    } 

Q)      why sun micro systems designed println() method in this way?
 Ans)  Because at any point of time only one thread can be inside of this block,
                                     
Q)     Then what is the use?
Ans)   To prevent form the deadlock.If multiple thread access the same code it becomes a scrap.

In this way the println() method is a best example of Synchronization.

Thanks,

Rajesh Kumar Yuvaraj



Redirecting the output of System.out.println()

Hi Guys,

Using the below example we can redirect the out put of the System.out.println().

public class ChangeOut {
  public static void main(String args[]) {
    try {
      System.setOut(new PrintStream(new FileOutputStream("log.txt")));
      System.out.println("Now the output is redirected!");
    } catch(Exception e) {}
  }
}

Thanks

Rajesh Kumar yuvaraj

Wednesday, June 27, 2012

I Frame with javascript Example

Hi Guys,

           Here is a sample iFrame example.



<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <title>Untitled 1</title>

        <script >
            function go(loc){
                alert(loc);
                document.getElementById('calendar').src = loc;
            }
        </script>
    </head>

    <body>
        <iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="2" scrolling="no"></iframe>

        <form method="post">
            <input name="calendarSelection" type="radio" onclick="javascript:go('https://google.com')"/>Day
        </form>

    </body>

</html>


Thanks,


Rajesh Kumar Yuvaraj

Tuesday, June 12, 2012

Allow only Alphabets in the textfield

Hi,



       function AllowAlphabet(e)
            {
                isIE=document.all? 1:0
                keyEntry = !isIE? e.which:event.keyCode;
                if(((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry=='46') || (keyEntry=='32') || keyEntry=='45' )
                    return true;
                else
                    return false;
            }
Usage :
 <input type="text" onkeypress="return AllowAlphabet(event)">

Struts 2 examples

Hi,

http://shivasoft.in/blog/java/struts/step-by-step-simple-login-application-in-struts-2/

Monday, June 11, 2012

Numbers Only logic


Hi,

Usage : onkeypress ="return  numbersonly(event);"

function numbersonly(e) {
 var keynum
 var keychar
 var numcheck


 if(window.event) { // IE
  var code = window.event;
    keynum = code.keyCode
 } else if(e.which) {// Netscape/Firefox/Opera
  keynum = e.which
 }

 if (keynum == 99) return true;
 if (keynum == 118) return true;
 //if (keynum == 42) return true;
 if (keynum == 8 || keynum == 13 ) return true;
 if (keynum < 48 || keynum > 57 ) return false;
}

Saturday, June 9, 2012

How to get the session contents of a web applications.

Hi Guys,

            Here is a sample code which is used to get the session contents of any web container.

<!-- Displays the session content in any of the web application.
@author  : Rajesh
-->
<body >
<style>
    body{ font-family:Tahoma, sans-serif, Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0px; padding:0px; background-color:#A7C71A; color:#FFFFFF;}
    .actionButton{border:1px solid #d7e3ef;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;background: -moz-linear-gradient(top,  #fbbc79,  #f98f15);background: -webkit-gradient(linear, left top, left bottom, from(#fbbc79), to(#f98f15));filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fbbc79', endColorstr='#f98f15');display:inline-block;color:#333;padding:4px 10px;font-weight:700;text-shadow:1px 1px #f1f1f1;text-decoration:none;margin:2px;font-size:13px;}
</style>
<center>
    <u>
        <h2>
        </h2>
    </u>
    <table><tr><td>
                <form name="frm1" method="POST" action="./test.jsp?clearsession=true">
                    <input class="actionButton" type=submit value="Clear Session">
                </form>

            </td>
            <td>
                <form name="frm2" method="POST" action="./test.jsp">
                    <input class="actionButton" type=submit value="Get Session">
                </form>
            </td>
        </tr>
    </table>


</center>
<%@page import= "java.util.*" %>
<%
            if (request.getParameter("clearsession") != null && request.getParameter("clearsession").equals("true")) {
                session.invalidate();
                return;
            }
            String[] keys = (String[]) session.getValueNames();
            int i = 1;
%>
<table border="1" align="center" cellpadding="4" cellspacing="4">
    <tr>
        <th> Sl.No </th>
        <th> Attribute Name </th>
        <th> Attribute Value </th>
    </tr>
    <% for (String key : keys) {
    %>
    <tr>
        <td> <%=i%></td>
        <td><%=key%></td>
        <td><%=session.getAttribute(key)%></td>
    </tr>
    <%
                    i++;
                }
    %>
</table>

Thanks,

Rajesh Kumar Yuvaraj