25 Dec 2011

JavaScript in JSP : Bad Practice

A JSP(Java Server Pages) is a server side technology whereas the JavaScript is a client side. Even after both these are work on different side , they work together to create more dynamic responsive pages on the world wide web. I had initiated a discussion ,whether  is it a bad practice to use JavaScript within JSP pages ,on the  javaranch for which i get great response. Here are some quotes of the discussion : 


Paul Clapham said
"As long as you realize that the Javascript code you put in your JSP is not going to be executed as part of the JSP, it's going to be executed only when it gets to the browser i.e. on the client side. That seems to be a concept that a lot of people aren't told about when they start learning JSP. As already pointed out, it's better to put your Javascript in a separate file and just provide a link to that file in your JSP. That way the browser can cache the Javascript instead of having to download it repeatedly as part of the JSP. There are cases, though, where you might want your JSP to generate some Javascript which varies based on input parameters or session data or whatever. "
 That seems reasonable if something is there to use  then use it ,as simple as that. But some fellow has different opinion about that.Lets see what Tim has said 


Tim Holloway said
"JavaScript is an unholy mess, but it can make pages more interactive because it programs the client instead of the server.You shouldn't depend on JavaScript to be your sole data validator. The final determination of what's valid should be in the server-side code, where it cannot be spoofed by a malicious client. Plus, people do sometimes turn JavaScript off in their browsers, in which case none of the client-side code will execute at all!"
a very important point is been noted here since JavaScript is run inside browser and browser can turn off the JavaScript so one must be aware of that and action must be taken on server side.  

Bear Bibeault said
a JSP is simply a template for an HTML page. To the JSP processor, anything that's not a JSP construct is just template text that is completely ignored by the JSP engine and sent to the client verbatim. So JavaScript is completely opaque to the JSP. Now, putting Java scriptlets into a JSP: that's not only a poor practice, but completely irresponsible at this point in time when we're coming up to the 10th, yes 10th, anniversary of JSP 2.0. Ten years is plenty of time for anyone to have come up to speed on the JSTL and EL and there is no excuse, none, nada, zero zilch, justification for still writing JSPs as if it were 2001. 



see full discussion  here

22 Dec 2011

JSTL library problem

i was creating a web application on  the Eclipse IDE using the GlassFish server.when i trying to use the JSTL library by 
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %> in my jsp file.
But what i get is 
org.apache.jasper.JasperException:According to TLD or attribute directive in tag file, attribute value does not accept any expressions.


The root cause of the error is that the JSTL's uri is incorrect for the used jsp version.

Make Correct use of Library :
1. Check the servlet version and refer to it in the Deployment Descriptor(web.xml) of web application.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

2. Refer correct JSTL uri for the servlet or jsp.
servlet_2.3 or jsp_1.2 => JSTL_1.0 (JSTL uri =http://java.sun.com/jstl/core)
servlet_2.4 or jsp_2.0 => JSTL_1.1 (JSTL uri =)http://java.sun.com/jsp/jstl/core
servlet_2.5 or jsp_2.1 => JSTL_1.2 (JSTL uri =http://java.sun.com/jsp/jstl/core

)

1 Dec 2011

Passed OCPJP 6


Wait is over  yesterday i passed my OCPJP 6 exam. It has been long since i have been thinking about to give a shot at it. The exam tests ones basic concepts about java programming which were categorized into different objectives. The exam covers all of the specified objectives. The question were easy once one is through with the concepts.

The Bible book for the exam is SCJP 6 by Kathy Sierra and Bert Bates . The book explains a topic in most simple and effective to build a concept.It is very important to write small code to learn a topic ,tweak with & play with it , i wrote about 300 small programs on different topics .The wonderful place for a java lover is javaranch this a place where i clear my doubts and also learn new things.Two weeks before the exam i started doing mocks available on line after given exam i think question set given at end of  "The Bible" is enough to practice.

So it was a good experience as to gain global certification and getting the grasp on the very basics of java programming and of course personal satisfaction is there.




7 Oct 2011

Java Innovation 2011


The Duke's Choice Awards celebrate extreme innovation in the world of Java technology and are granted to the most innovative projects using the Java platform.

The most innovative projects from 2011 using the Java platform include:


Innovative Integration Testing

Arquillian Project Arquillian is a platform for simplifying Java integration testing. Arquillian transparently handles the pesky scaffolding like server startup, framework initialization and deployment – leaving you to focus on the task at hand: writing your test logic.


Innovative Green Technology

dooApp Infiltrea is an end-to-end solution, built with JavaFX and Java Platform, Standard Edition (Java SE), designed for green building professionals whose job is to measure the air tightness of buildings.


Innovative Industrial Software

Inductive Automation Ignition is a Java-based web application that includes a business scorecard, to integrate different manufacturing machines using a central web-server. It creates systems that cover the full spectrum between Human Machine Interface (HMI), Supervisory Control and Data Acquisition (SCADA) and Manufacturing Execution System (MES), enabling rapid project development and deployment.

Innovative Home Automation

Java Platform, Enterprise Edition (Java EE) jHome A complete home automation API open-source for Glassfish/JavaEE enabling developers to control anything in their homes such as lamps, LED light, coffee machines.

Innovative Tool for Developers

JFrog JFrog's Artifactory is the world's first binary repository manager. Built with the Content Repository API for Java (JCR) specification, it is helping change the way developers build and manage software modules by delivering high concurrency and unmatched data integrity.

Innovative Compiler for Java Code

JRebel JRebel is a Java Virtual Machine (JVM) plug-in that enables Java developers to instantly see any code change made to an application. It allows developers to skip the build and redeploy phases and has prevented 39 million redeploys.

Innovative Programming Framework

LMAX The LMAX Disruptor is a multi-threaded, open-source concurrent programming framework designed for high-performance and low-latency transaction processing. Part of LMAX's Java-based trading platform, the LMAX Disruptor replaces and outperforms java.util.concurrent.ArrayBlockingQueue by up to 80x.

Innovative Industrial Automation

Rockwell Automation Rockwell Automation is creating its next generation, Java-enabled HMI device line of products that will allow for the automated communication and exchange of data to factory floor lines.

Innovative Accessibility Solution

Sodbeans Project The Sodbeans Project is an open-source, NetBeans-based module suite designed to enhance accessibility for the blind in modern programming environments. The NetBeans module suite includes a new programming language designed for ease-of-use, a text-to-speech engine designed to make NetBeans compatible with screen readers on multiple platforms and a number of features designed to make programming easier for the blind.

Innovative Network Programming

The Netty Project The Netty Project is a Java-based new I/O (NIO) client server framework that enables quick and easy development of network applications. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

To learn more ,Please visit

25 Sept 2011

Finding Class Files


The java run time environment look for the Bootstrap, Extension and User-defnied or third-party classes, in that specific order.
Lets look what and where are they  

Java run time environment look for the classes that are required for Java Platform  in  
<java-home>jre/lib/  directory and  these classes are called Bootstrap classes.
More specifically JVM looks for rt.jar and i18n.jar files.
 Location of these files are stored in sun.boot.class.path System property.
SDK Tools such as javac and java are located at <jdk-ome>/lib/tools.jar file



Classes, which extends the Java Platform , can be found in <java-home>jre/lib/ext/ directory. These classes are called  Extension classes. For example jar file required for the specific database for using the JDBC must be stored here.  

The directory in which these classes are located is platform specific.


User-defined or Third party class or jar files required by the application must be located in the classpath variable. A classpath variable points to the directory of required files .By default current directory is set as a classpath variable. One can set the classpath using command line option –cp or by setting the CLASSPATH environment variable. Both these methods override the default value of classpath variable.
It must be noted that user is not required to set the classpath for Bootstrap and Extension classes

Multiple paths can be specified in classpath entry by  using the system dependent path separator . In that case ,specified order is important because in JVM will look for class files in that specific order.

18 Aug 2011

JDK 7 features



Java Development Kit (JDK) 7 is most recent release of Java Standard Platform. I takes the birth at the OpenJDK project and has taken collaborative effort of Sun and Java developer community.


JVM
  • Extensions to the JVM  to support the implementation of dynamically-typed languages (InvokeDynamic) at performance levels near to that of the Java language itself. 

Programming Language
  • The integral types (byteshortint, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
  • The String class can be used in the expression of a switch statement.
  • A single catch block can handle more than one type of exception. In addition, the compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE.
  • The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements the new java.lang.AutoCloseable interface or the java.io.Closeable interface can be used as a resource. 
  • The type arguments required to invoke the constructor of a generic class can be replaced with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context.
  • The Java SE 7 complier generates a warning at the declaration site of a varargs method or constructor with a non-reifiable varargs formal parameter.
  
Networking 
  • The URLClassLoader.close() method effectively eliminates the problem of how to support updated implementations of the classes and resources loaded from a particular codebase, and in particular from JAR files.
  • Sockets Direct Protocol support for reliable, high-performance network streams over Infiniband connections on Solaris and Linux.

Concurrency Utilities
  • The fork/join framework is designed to efficiently run a large number of tasks using a pool of worker threads.
  • The ThreadLocalRandom class eliminates contention among threads using pseudo-random numbers.
  • The Phaser class is a new synchronization barrier.

I/O
  • New APIs for filesystem access, scalable asynchronous I/O operations, socket-channel binding and configuration, and multicast datagrams.
  • A fully-functional and supported NIO.2 filesystem provider for zip and jar files.

JDBC
  • The ability to use a try-with-resources statement to automatically close resources of type ConnectionResultSetand Statement.
  • RowSet 1.1: The introduction of the RowSetFactory interface and the RowSetProvider class, which enable you to create all types of row sets supported by your JDBC driver.


    6 Aug 2011

    Under the Hood : Java Program Execution


    Starting JVM
    By invoking the main() of a class and passing argument to it. But at this stage main() still not invoked ,because JVM does not have binary representation of the class.


    Loading class or interface.
    JVM retrieve the binary representation of a class or interface from compiled source code and constructing the .class file format.

    Loading is implemented by the ClassLoader and its subclasses.
    If any error occurs during loading  , it throws instance of LinkageError or its subclasses.
    Array classes do not have an external binary representation; they are created by the Java virtual machine rather than by a class loader.


    Linking {Verification, Preparation, Resolution} class or interface
    Linking is a process of taking binary form of class or interface and combining it to run-time state of the Java virtual machine.
     Verification ensures that the binary representation of a class or interface is structurally correct.
    For example, it checks that every instruction has a valid operation code, every method is provided with a structurally correct signature etc.
    If an error occurs during verification, , it throws instance of LinkageError or its subclasses.
    Preparation involves creating the static fields for a class or interface and initializing such fields to the standard default values or explicit .
    Resolution is the process of checking symbolic references from class other classes and interfaces, by loading the other classes and interfaces that are mentioned and checking that the references are correct.
    If an error occurs during Resolution, it throws instance of IncompatibleClassChangeError or its subclasses


    Initialization class or interface
    Initializes the static initializer block and static fields but final  static field are not initialized at  this stage because they are complie-time constant..
    Initialization occur before any instance is created or any static method is invoked.
    Once a class is initialized  its main() is invoked by JVM.
    Before a class or interface is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized.
    the superinterfaces of an interface need not be initialized before the interface is initialized.


    Instantiation of class
    When new() operator is encountered a instance is created.
    Memory is allocated for instance variable of class and that of its superclass as well.
    If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError.


    Unloading class and interface.
    A class or interface may be unloaded if and only if its class loader is unreachable.
    The bootstrap class loader is always reachable; as a result, system classes may never be unloaded.


    JVM Exists
    The Java virtual machine terminates all its activity and exits when one of two things happens: 
    All the threads that are not daemon threads terminate.
    Some thread invokes the exit method of class Runtime or class System, and the exit operation is permitted by the security manager.