Wednesday, February 23, 2011

Can you Directly get the Browser Name and Version in the JSP?

Hai Guys,

There is no direct way to the get the Browser Name and version in the JSP.

check the following program.

copy the following code inside a JSP page.
<%!
String browserName = "";
String browserVersion = "";
String tmpResult = "";
String getBrowserVersion(String ua, int index) {
tmpResult=ua.substring(index,ua.length());
String temp[]=tmpResult.split(" ");
return temp[0].replaceAll(";","");
}
%>
<%
String _ua = (String) request.getHeader("User-Agent");
System.out.println(_ua);
if (_ua.contains("MSIE")) {
browserName = "Microsoft Internet Explorer";
browserVersion = getBrowserVersion(_ua, _ua.indexOf("MSIE ")+5);
}
if (_ua.contains("Firefox")) {
browserName = "FireFox";
browserVersion = getBrowserVersion(_ua, _ua.indexOf("Firefox/") +8);
}
if (_ua.contains("Chrome") && _ua.contains("Safari")) {
browserName = "Google Chrome";
browserVersion = getBrowserVersion(_ua, _ua.indexOf("Chrome/") +7);
}
if (_ua.contains("Safari") && _ua.indexOf("Chrome")==-1) {
browserName = "Safari";
browserVersion = getBrowserVersion(_ua, _ua.indexOf("Version/") +8);
}
out.print("BrowserName  : " + browserName + "<br>");
out.print("BrowserVersion  : " + browserVersion);
 %>
out put

BrowserName : FireFox
BrowserVersion : 3.6.13

 OR

 <%!     
String browserName = "";
String browserVersion = "";
String tmpResult = "";
String getBrowser(String ua, String splitcon, String name) {
String temp1[] = ua.split(splitcon);
for (String s : temp1) {
if(s.contains(name))
tmpResult = s.trim();
}
tmpResult = tmpResult.replaceAll(" ", "/");
String temp2[] = tmpResult.split("/");
return temp2[1];
}
%>
<%
String _ua = (String) request.getHeader("User-Agent");
if (_ua.contains("MSIE")) {
browserName = "Microsoft Internet Explorer";
browserVersion = getBrowser(_ua, ";", "MSIE");
}
if (_ua.contains("Firefox")) {
browserName = "FireFox";
browserVersion = getBrowser(_ua, " ", "Firefox");
}
if (_ua.contains("Chrome") && _ua.contains("Safari")) {
browserName = "Google Chrome";
browserVersion = getBrowser(_ua, " ", "Chrome");
}
if (_ua.contains("Safari") && _ua.indexOf("Chrome")==-1) {
browserName = "Safari";
browserVersion = getBrowser(_ua, " ", "Version");
}
out.print("BrowserName  : " + browserName + "<br>");
out.print("BrowserVersion  : " + browserVersion);
%>

OUT PUT

BrowserName : FireFox
BrowserVersion : 3.6.13

Thanks,

Keep Rocking.

Sunday, February 13, 2011

JAVA videos...

check this for java videos...

http://www.java-videos-promotions.blogspot.com/

Thursday, February 10, 2011

Use of jps Command?

Good Day Friends..
if u want to know about the currently running JAVA processes here is a command


NAME
          jps - Java Virtual Machine Process Status Tool

SYNOPSIS
       jps [options] [hostid]

DESCRIPTION
       The  jps  tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions.

       If jps is run without specifying a hostid, it will look for instrumented  JVMs  on  the  local  host.  If started  with  a  hostid,  it  will look for JVMs on the indicated host, using the specified protocol and port.  A jstatd process is assumed to be running on the target host.

       The jps command will report the local VM identifier, or lvmid, for each instrumented  JVM  found  on  the target system. The lvmid is typically, but not necessarily, the operating system’s process identifier for the JVM process. With no options, jps will list each Java application’s lvmid followed by the short  form
of the application’s class name or jar file name. The short form of the class name or JAR file name omits
       the class’s package information or the JAR files path information.

       The jps command uses the java launcher to find the class name and arguments passed to the main method. If the  target JVM is started with a custom launcher, the class name (or JAR file name) and the arguments to  the main method will not be available. In this case, the jps command will output the string  Unknown  for the class name or JAR file name and for the arguments to the main method.

       The  list  of JVMs produced by the jps command may be limited by the permissions granted to the principal running the command. The command will only list the JVMs for which the principle  has  access  rights  as determined by operating system specific access control mechanisms.

       NOTE  - This utility is unsupported and may or may not be available in  future versions of the JDK.  It is not currently available on Widows 98 and Windows ME platforms.

Use of jmap command?

hey Guys if you want to know the secrets of the JVM and its behavior here is a useful command.

which will give you the important information about the JVM memory Mappings.

       jmap prints shared object memory maps or heap memory details of a given
       process or core file or remote debug server.

       NOTE - This utility is unsupported and may or may not be  available  in
       future  versions  of  the J2SE SDK.  jmap is not currently available on
       Windows platforms or on the Linux Itanium platform.

 Usage

     jmap [option] pid
     jmap [option] executable core
     jmap [option] [server-id@]remote-hostname-or-IP


OPTIONS

<no option>  When  no  option is used jmap prints shared object mappings. For each shared object  loaded in the target VM, start     address, the size of the mapping, and the full path of the  shared
                      object file are printed. This is similar to the Solaris pmap utility.
-heap             Prints  a  heap  summary.  GC  algorithm used, heap configuration and generation wise heap
                      usage are printed.

 -histo            Prints a histogram of the heap. For each Java class, number of  objects,  memory  size  in
                      bytes,  and  fully  qualified class names are printed. VM internal class names are printed
                      with ’*’ prefix.
-permstat      Prints class loader wise statistics of permanent generation of Java heap. For  each  class
                      loader,  its  name,  liveness,  address,  parent  class loader, and the number and size of
                      classes it has loaded are printed.
 

 -h                  Prints a help message.
Example  


jmap -heap 2709


using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration:

   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 67108864 (64.0MB)
   NewSize          = 655360 (0.625MB)
   MaxNewSize       = 4294901760 (4095.9375MB)
   OldSize          = 1441792 (1.375MB)
   NewRatio         = 12
   SurvivorRatio    = 8
   PermSize         = 8388608 (8.0MB)
   MaxPermSize      = 67108864 (64.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 589824 (0.5625MB)
   used     = 384136 (0.36634063720703125MB)
   free     = 205688 (0.19615936279296875MB)
   65.12722439236111% used
Eden Space:
   capacity = 524288 (0.5MB)
   used     = 382816 (0.365081787109375MB)
   free     = 141472 (0.134918212890625MB)
   73.016357421875% used
From Space:
   capacity = 65536 (0.0625MB)
   used     = 1320 (0.00125885009765625MB)
   free     = 64216 (0.06124114990234375MB)
   2.01416015625% used
To Space:
   capacity = 65536 (0.0625MB)
   used     = 0 (0.0MB)
   free     = 65536 (0.0625MB)
   0.0% used
tenured generation:
   capacity = 7544832 (7.1953125MB)
   used     = 6702600 (6.392097473144531MB)
   free     = 842232 (0.8032150268554688MB)
   88.83696813925081% used
Perm Generation:
   capacity = 8388608 (8.0MB)
   used     = 4809184 (4.586395263671875MB)
   free     = 3579424 (3.413604736328125MB)
   57.32994079589844% used

Keep Rocking Guys.....

Monday, February 7, 2011

Why hashCode() can return the same value for different objects in java?

Hai guys i have some interesting thing related to the HashCode of java Objects....

                     The point is that hashcodes can be the same without necessarily guaranteeing that the objects are equal, because the "hashing algorithm" used in the hashCode() method might happen to return the same value for multiple objects"

Q. Do all the Objects of the same class will have the Same HashCode?
A. No

Q. Can different objects can have the same HashCode?
A. May or may not

                  Just consider a  scenario where i am using the HashSet which uses the Hash code to store the  Objects of any class, if i am adding the two different objects which have the same hashCode  and don't forget that  HashSet will not allow the duplicate elements and specifically it uses the hashcode to store and access???????


Typically, this does not produce any problems, because hashCode() is mostly used together with equals(). For instance, a HashMap will call hashCode() upon its keys, to know whether the keys may already be contained in the HashMap. If the HashMap does not find the hash code, it's obvious the key is not contained in the HashMap yet. But if it does, it will have to double-check all keys having that same hash code using equals().
I.e.
A.hashCode() == B.hashCode() // does not necessarily mean
A.equals(B)
but
A.equals(B) // means
A.hashCode() == B.hashCode()
if equals() and hashCode() are implemented correctly