Posts

Showing posts from October, 2013

what is Ajax, Why use Ajax and How to implement in Java

Some use for Ajax interactions are following: 1. Real time form data validation e.g. UserID, EmailID, Serial No, Postal Code etc. 2. Load on demand e.g. fetch data in background, allowing browser load page quickly 3. Sophisticated user interface controls and effects e.g. control such as calendar, trees, menus, data tables ,rich text editor. 4. Refreshing data and server path e.g. such as scores, stock quotes, weather, or application specific data. 5. Partial submit 6. Page as an application e.g. single page application that look and feels like desktop application. XMLHttpRequest: A JavaScript object, it allow client-side script to perform HTTP Request, and it will parse an xml server response. Ajax stands for asynchronous; when you send that HTTP request you don’t want the browser to hand around waiting for the server to respond. Instead, you want to continue reading to the user’s interaction with the page and deal with the server’s response when it eventu...

Working Example of Hibernate with explanation

A Hibernate has mainly three components: 1. Hibernate Mapping File (*.hbm.xml) 2. Hibernate Configuration File (*.cfg.xml) 3. Persistence  Object (*.class) Mapping File: Mapping  the Contact Object to the Database Contact table The file contact.hbm.xml is used to map Contact Object to the Contact table in the database.  Here is the code for contact.hbm.xml: contact table must has ID, firstname, lastname, email field as describe in mapping file <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jan 1, 2002 12:34:16 AM by Hibernate Tools 3.2.1.GA --> <hibernate-mapping>     <class name="hibernateappexample.Contact" table="contact" catalog="stu">         <id name="id" type="int">             ...

State-Management in java web application with example

State-Management: HTTP protocol that is used on Internet to send request to receive response is a stateless protocol. I.e. for each request, new http connection is created between client and server and this connection is closed as soon as last byte of response is received by the client. Q. Why used stateless protocol? A.  Proper utilization of network Following four methods: Cookies: Cookies are small bits of textual information that a web server sends to a browser and that the browser later returns unchanged when visiting the same web site or domain. Non persistent: by default are non persistent that is that are lost as soon as communication with the server is stop. Persistent: persistent cookies are saving in the form of text file by the browser to be reuse when the communication with the server is stop. Limitation: Browser dependent method. All the browsers do not’s support cookies for the cookies can be disable from the browser. Only text information can be pers...

How many way session can die ?

Three ways a session can die:- a.    If time out b.    You call invalidate() on the session object c.    The application goes down(crashes or is underplayed) Configure a timeout in the DD has virtually the same effect as calling setMaxInactiveInterval() on every session that’s created. <web-app>     <servlet>     </servlet> <session-config> <session-timeout>30</session-timeout> </session-config>     </web-app>     session.setMaxInactiveInterval(seconds); session.invalidate():- end the session. This includes unbinding all session attributes currently stored in this session. Some points:     One problem how does the Container know who the client is?     The client needs a unique session id-on the client first request, the container generates a unique session ID and gives it back to the c...

What is Servlet and Why needs Servlet ?

Servlet: In begining internet consists only static contents written using HTML. Soon dynamic web contents were made possible using CGI technology. CGI enable the web server to call an external program and pass HTTP request information to that external program to process the request. The response from external program to pass back to the web server, which forwards it to the client browser. When no. of users visiting popular site, CGI had failed to deliver scalable internet application. Web application development technologies: ColdFusion, Server Side Java Script (SSJS), PHP, Servlet, Jsp, Asp, Asp.net. Now, Asp (Asp.net) and Servlet/jsp have been the main technology. A Servlet is a java program that programmatically extends the functionality of a web server in request-response programming model. Servlets are responsible for generating dynamic html. Servlet is executing within a web container and it does not have a visible interface. Servlets are managed objects that are object of ...

what is J2EE ?

J2EE-Java 2 Enterprise Edition: J2EE provide a platform for developing a platform independent, portable, multiuser, secure, and distributed server-side application. EJB is a portion of J2EE. Java 2 platform for enterprise application.J2EE is a specification their implementation is provided by the vendors in the form of application server or web server. In J2EE, Sun Microsystem Provided concept of application server. An Application server is a software package that contains:- (1)    Web container – To execute web application (2)    EJB container – To execute EJB modules (3)    JNDI server – To facilitated registration and searching of resources (4)    Messaging server – To facilitated asynchronous message passing between different components of an enterprise application. (5)    UDDI server – To store and look up information about web services Commonly used Application servers:- Web logic server Sun a...

Boxing and Unboxing in java

Boxing and Unboxing: Suppose we want an array list of integers. Unfortunately, the type parameter inside the angle brackets cannot be a primitive type. It is not possible to form an ArrayList<int>. Here, the Integer wrapper class comes in. It is ok to declare an array list of Integer objects. ArrayList<Integer> list = new ArrayList<Integer>(); Note: An ArrayList<Integer> is far less efficient than an int[] array because each value is separately wrapped inside an object. You would only want to use this construct for small collections when programmer convenience is more important than efficiency. The call list.add(3); is automatically translated to list.add(new Integer(3)); This conversion is called autoboxing. Conversely, when you assign an Integer object to an int value, it is automatically unboxed. That is, the compiler translates int n = list.get(i); into int n = list.get(i).intValue();

Runtime polymorphism and dynamic binding

Runtime polymorphism and dynamic binding: Resolving a method call that is finding out which method definition is to be executed by a method call is called binding. Binding is down by the compiler. In normal cases compiler identify a method definition for a method call at compilation time that is method call is resolved at compilation time. Resolving a method call at compilation time is static binding. In case of method overloading static binding is performing. “Reference variable of a base class can contain the references of its sub class objects.” Class A{ Public void display() { s.out.println(“display of A”); } } Class B extends A{ Public void display(){ s.out.println(“display of B”); } } Class c extends A { Public void display(){ s.out.println(“display of C”); } } Class D { Public static void main(String as[]){ A x=new A(); B y=new B(); C z=new Z(); callMe(x); callMe(y); callMe(z); } Private static callMe(A p){ p.display(); } } “If a method call ...

Thread and their state

A Thread is a single sequence of execution that can run independently in an application. Knowledge of threads in programs is useful in term of resource utilisation of the system on which an application is running. Multithreaded programming is very useful in network and internet applications developments. Multithreaded programs support more than one concurrent thread of execution. This means they are able to simultaneously execute multiple sequences of instructions. Each instruction sequences have its own unique flow of control that is independent of all others. These independently executed instruction sequences are known as threads. In single processor system, only a single thread of execution occurs at a given instant, but multiple threads in a program increase the utilization of CPU.

Implementation of Join() method of Thread

Implementation of Join() public class MyReader {     BufferedReader b;     public MyReader() {         b = new BufferedReader(new InputStreamReader(System.in));     }     public String readData(String msg){         String str;         try{             System.out.println(msg);             str=b.readLine();             return str;         }catch(Exception e){System.out.println(e);}         return null;     } } public class NameThread extends Thread{ final MyReader r; public NameThread(MyReader reader){     r=reader; }     @Override public void run(){         synchronized(r){        ...

Thread Synchronization

Thread Synchronization: Execution of a thread is asynchronous by nature that is context switch between thread can not be predicted in advance.  Synchronization is desirable when multiple threads share a common resource in order to use the resource mutually exclusive manner. E.g. two users did not access the same bank account simultaneously. “Who thread when execute is not predicated; only no. of thread is fixed.” Synchronization is achieved in two ways:- 1.    Synchronized method One or more methods of a class can be declared to be synchronized. When a thread calls an object’s synchronized method, the whole object is locked. This means that if another thread tries to call any synchronized method of the same object, the call will block until the lock is released (which happens when the original call finishes). synchronized returnType methodName(if arg any){ //statements } 2.    Synchronized block There are cases where we need ...

Inter Thread communication

Inter Thread communication Java provides three methods that threads can use to communicate with each other:  public final void wait()throws InterruptedException  public final native void notify(),  public final native void notifyAll(). These methods are defined for all objects (not just Threads). The idea is that a method called by a thread may need to wait for some condition to be satisfied by another thread. In that case, it can call the wait method, which causes its thread to wait until another thread calls notify or notifyAll. A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify). wait(long timeout...

Difference between Multithreading and Multiprocessing

Multiprocessing: ü  To obtain maximum throughput ü  To reduce average execution time of process A Thread is a single sequence of execution that can run independently in an application. Multithreaded programs support more than one concurrent thread of execution Multithreading represents concurrent execution of multiple threads. Multithreading is a lightweight version of multiprocessing i.e. less over head is incurred by the o.s. in multithreading as compare to multiprocessing. Difference between Multithreading and Multiprocessing ü  In multiprocessing each process represents an independent application where as in multithreading each thread represents an independent module of an application. ü  Each process as it own addresses space where as all the threads in an application share a common address space. Difference between Thread and Process: ü  Thread share address space of the process that created it; processes have their own address space...