MY mENU


Thursday 29 March 2012

JSTL Types

JSP standard tags library can be divided into four tag libraries which are:
  1. Core tags
  2. Internationalization (i18l) and formatting tags
  3. Relational database access tags
  4. XML processing tags
The goals of those tag library above are:
  • Simplify the task of writing JSP page by providing friendly XML base tags
  • Provide reusable logic from page's presentation
  • Make the JSP page easier to read and maintain
Core tags:
As its name imply, core tags provide the core functionality actions to JSP to make the most common actions easier to achieve in a more effective way. Core tags specify several actions such as displaying content based on condition, manipulating collections and URL managing. By using the core tags you'll never has to write a such small piece of scriptlet. (But you still need to know scriptlet to maintain legacy web applications and convert them to JSTL later on if you have to).

Internationalization(I18L) and formatting tags:
Those tags specify a series actions to make the web application multilingual. Those actions including managing resource bundle, locales and base names.

Relational database access tags:
Accessing database is a most major task of web applications. JSTL provides a list of standard tags to help you to manipulate data such as select, insert, update and delete from the relational databases.

XML processing tags:
XML becomes a standard of enterprise web application for exchanging data. ManipulateXML effectively therefore is very important for most web applications and of course JSTL also provides a list of tags for processing from XML parsing to XML transformation.

Introducing to JSP Standard Tag Library - JSTL

JSP is designed for the presentation layer in the web applications but it needs to contain the logic or code inside the page to control the way it presents the visual elements. Since JSP was borned, scriptlet has been used intensively therefore the JSP pages become messy and difficult to maintain. The HTML mixed with JSP scriptlet and opening and closing brace make JSP page even hard to extend.

In June 2002, JavaServer Pages standard library (JSTL) specification 1.0 was first released. JSTL provided a new ways for JSP author to work with different elements with standard friendly tags. The current version of the JSTL is 1.2 which was started in 2004 so we will use JSTL 1.2 for all the tutorials.

JSP Advantages

  1. Separate the business logic and presentation: The logic to generate dynamic elements or content is implemented and encapsulated by using JavaBeans components. The user interface (UI) is created by using special JSP tags. This allows developers and web designers to maintain the JSP pages easily.
  2. Write Once, Run Anywhere: as a part of Java technology, JSP allows developers to develop JSP pages and deploy them in a variety of platforms, across the web servers without rewriting or changes.
  3. Dynamic elements or content produced in JSP can be served in a different formats: With JSP you can write web application for web browser serving HTML format. You can even produce WML format to serve hand-held device browsers. There is no limitation of content format which JSP provides.
  4. Take advantages of Servlet API: JSP technically is a high-level abstraction of Java Servlets. It is now easier to get anything you've done with Servlet by using JSP. Beside that you can also reuse all of your Servlets you've developed so far in the new JSP.

Introduction to JSP

JSP stands for JavaServer Pages. JSP is one of the most powerful, easy-to-use and fundamental technology for Java web developers. JSP combines HTML, XML, Java Servlet and JavaBeanstechnologies into one highly productivetechnology to allow web developers to develop reliable, high performance and platform independent web applications and dynamic websites.

Creating Threads in Java

Different ways to Create custom Threads in Java:
in java we can create user defined threads in two ways

  • Implementing Runnable Interface
  • Extending Thread class
in the both two approaches we should override run() method in sub class with the logic that should be executed in user defined thread concurrently, and should call start() method on Thread class object to create thread of execution in java Stacks area.

1. Extending the Thread class.::
class Mythread extends Thread {
                                  //method where the thread execution will start
public void run(){
                             //logic to execute in a thread
}
                               //let’s see how to start the threads
public static void main(String[] args){
Mythread t1 = new Mythread();
Mythread t2 = new Mythread();
t1.start();          //start the first thread. This calls the run() method.
t2.start();         //this starts the 2nd thread. This calls the run() method.
}
}

2. Implementing the Runnable interface.::
class MyRunnable extends Base implements Runnable{
                                      //method where the thread execution will start
public void run(){
                                     //logic to execute in a thread
}
                                    //let us see how to start the threads
public static void main(String[] args){
Thread t1 = new Thread(new MyRunnable());
Thread t2 = new Thread(new MyRunnable());
t1.start();                    //start the first thread. This calls the run() method.
t2.start();                   //this starts the 2nd thread. This calls the run() method.
}
}


In the first approach we create subclass object , Thread class object is also created by using its no-arg constructor. then when start method is called using subclass object custom thread is created in java stacks area, and its execution is started based on processor busy.

In the second approach when we create subclass object , Thread class object is not created, because it is not a subclass of Thread. so to call start method we should create thread class object explicitly by using Runnable parameter constructor, then using this Thread class object we should call start method. Then custom thread is created in Java stacks area and its execution is started based on processor busy.

Process And Thread

A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.


Definition of Thread:

  • A Thread is an independent sequential flow of execution/path.
  • A Thread is a stack created in java stacks area.
  • It executes methods in sequence one after one

When MultiThreading programming is suitable- means need of Multithreading?

- To complete independent multiple tasks execution in short time we should develop multithreading. In multithreading based programming CPU ideal time is utilized effectively.

Which class is extended by all other classes?

 - The Object class is extended by all other classes.

Can an object be garbage collected while it is still reachable? - A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected

When can an object reference be cast to an interface reference?

An object reference be cast to an interface reference when the object implements the referenced interface.

What is an object’s lock and which object’s have locks?

An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.

What is the difference between a static and a non-static inner class?

 A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.

Which Java operator is right associative?

 - The = operator is right associative.
- Can a double value be cast to a byte? - Yes, a double value can be cast to a byte.

What is the argument type of a program’s main() method?

A program’s main() method takes an argument of the String[ ] type.

What is the purpose of the finally clause of a try-catch-finally statement?

The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.

What is the difference between the Boolean & operator and the && operator?

 If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand. When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

An object’s finalize() method may only be invoked once by the garbage collector.

What are order of precedence and associativity?

- Order of precedence determines the order in which operators are evaluated in expressions.
- Associativity determines whether an expression is evaluated left-to-right or right-to-left

What is a native method?

 A native method is a method that is implemented in a language other than Java.

What is the difference between preemptive scheduling and time slicing?

 Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

What modifiers may be used with an inner class that is a member of an outer class?

 A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

What is synchronization and why is it important?

 With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.