Click Here for Free Traffic!
Click Here for your Free Traffic!
Google
 

Monday, April 9, 2007

JAVA Tutorials

What is java?

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems.

First Java Program -HelloWorld.java


  HelloWorld class is an application that
"Hello World!" to the standard output.


public class HelloWorld {
// Display "Hello World!"
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
more>>

Objects in Java - Introduction

What is a Type?

A type is a category or grouping that describes a piece of data. Every data in Java can be identified by its type. Java has two general categories of types: primitives and classes.

Classes Describe Objects

Classes are the basic building blocks of Java programs. Classes can be compared to the blueprints of buildings. Instead of specifying the structure of buildings, though, classes describe the structure of "things" in a program.

//Contents of SomeClassName.java
[public] [( abstract | final )] class ClassName [extends ParentClass] [implements Interfaces]
{
// variables and methods are declared within the curly braces
}
  • A class can have public or default (no modifier) visibility.
  • It can be either abstract, final or concrete (no modifier).
  • It must have the class keyword, and class must be followed by a legal identifier.
  • It may optionally extend one parent class. By default, it will extend java.lang.Object.
  • It may optionally implement any number of comma-separated interfaces.
  • The class's variables and methods are declared within a set of curly braces '{}'.
  • Each .java source file may contain only one public class. A source file may contain any number of default visible classes.
  • Finally, the source file name must match the public class name and it must have a .java suffix.

public class Dog extends Animal implements Food
{
//Dog's variables and methods go here

What is an Object?

Objects are the physical instantiations of classes. They are living entities within a program that have independent lifecycles and that are created according to the class that describes them.

Attributes

A class describes what attributes an object will have but each object will have its own values for those attributes.

Behaviors

Behaviors are represented by methods. An object may call, or invoke, its own methods, or it may call another object's methods that are visible to it.

Constructors

In order to be used by a program, an object must first be instantiated from its class definition. A special type of method called a constructor is used to define how objects are created.

Messages

Objects cooperate and communicate with other objects in a program by passing messages to one another.

Class Inheritance via extends and implements

As you saw in the Horse example, a class can extend one other class and implement many Java interfaces. Extending and implementing is the Java mechanism for representing class inheritance.

more tutorials click here








Java Server Page

What is JSP?

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations. But most CGI variations, including servlets, make you generate the entire page via your program, even though most of it is always the same. JSP lets you create the two parts separately. Here's an example:
<html>
<head><title>Hello World JSP Page.</title></head>
<
body>
<font size="10"><%="Hello World!" %></font>
</body>
</html>


What are the Advantages of JSP?

JSP vs. Active Server Pages (ASP).
ASP is a similar technology from Microsoft. The advantages of JSP are two fold. First, the dynamic part is written in Java, not Visual Basic or other MS-specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

JSP vs. Pure Servlets.
JSP doesn't give you anything that you couldn't in principle do with a servlet. But it is more convenient to write (and to modify!) regular HTML than to have a zillion println statements that generate the HTML. Plus, by separating the look from the content you can put different people on different tasks: your Web page design experts can build the HTML, leaving places for your servlet programmers to insert the dynamic content.

JSP vs. Server-Side Includes (SSI).
SSI is a widely-supported technology for including externally-defined pieces into a static Web page. JSP is better because it lets you use servlets instead of a separate program to generate that dynamic part. Besides, SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.

JSP vs. JavaScript.
JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases, catalogs, pricing information, and the like.

JSP vs. Static HTML.
Regular HTML, of course, cannot contain dynamic information. JSP is so easy and convenient that it is quite feasible to augment HTML pages that only benefit marginally by the insertion of small amounts of dynamic data. Previously, the cost of using dynamic data would preclude its use in all but the most valuable instances.




Java Servlet Technology

what is a servlet?

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

Typical uses for HTTP Servlets include:

  • Processing and/or storing data submitted by an HTML form.

  • Providing dynamic content, e.g. returning the results of a database query to the client.

  • Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.

The Basic Servlet Architecture

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.

When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.

Life cycle






What are the Advantage of Servlets Over "Traditional" CGI?

Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and than many alternative CGI-like technologies. (More importantly, servlet developers get paid more than Perl programmers ).

Efficient.
With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N simultaneous request to the same CGI program, then the code for the CGI program is loaded into memory N times. With servlets, however, there are N threads but only a single copy of the servlet class. Servlets also have more alternatives than do regular CGI programs for optimizations such as caching previous computations, keeping database connections open, and the like.

Convenient.
Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such utilities.

Powerful.
Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can't). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations.

Portable.
Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server.

Inexpensive.
There are a number of free or very inexpensive Web servers available that are good for "personal" use or low-volume Web sites. However, with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. Nevertheless, once you have a Web server, no matter the cost of that server, adding servlet support to it (if it doesn't come preconfigured to support servlets) is generally free or cheap.

Thursday, April 5, 2007

Operators

Java Operators

Java operators produce new values from one or more operands (just so we’re all clear, the operands are things on the right or left side of the operator). The result of most operations is either a boolean or numeric value. And because you know by now that Java is not C++, you won’t be surprised that Java operators can’t be overloaded. There is, however, one operator that comes overloaded out of the box: If applied to a String, the + operator concatenates the right-hand operand to the operand on the left. Stay awake. The operators and assignments portion of the exam is typically the one where exam takers see their lowest scores. We aren’t naming names or anything, but even some of the exam creators (including one whose last name is a mountain range in California) have been known to get a few of these wrong.

Assignment Operators

Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the = to the variable on the left. Well, sure, but don’t expect to be tested on something like this:

x = 6;

No, you won’t be tested on the no-brainer (technical term) assignments. You will, however, be tested on the trickier assignments involving complex expressions and casting. We’ll look at both primitive and reference variable assignments. But before we begin, let’s back up and peek inside of a variable.

What is a variable? How are thevariable and its value related?

Variables are just bit holders, with a designated type. You can have an int holder, a double holder, a Button holder, and even a String[] holder. Within that holder is a bunch of bits representing the value. For primitives, the bits represent a numeric value (although we don’t know what that bit pattern looks like for boolean, but we don’t care). A byte with a value of 6, for example, means that the bit pattern in the variable (the byte holder) is 00000110, representing the 8 bits. So the value of a primitive variable is clear, but what’s inside an object holder? If you say

Button b = new Button();

what’s inside the Button holder b? Is it the Button object?

No! A variable referring to an object is just that—a reference variable. A reference variable bit holder contains bits representing a way to get to the object. We don’t know what the format is; the way in which object references are stored is virtual-machine specific (it’s a pointer to something, we just don’t know what that something really is). All we can say for sure is that the variable’s value is not the object, but rather a value representing a specific object on the heap. Or null. If the reference variable has not been assigned a value, or has been explicitly assigned a value of null, the variable holds bits representing—you guessed it—null. You can read

Button b = null;

as “The Button variable b is not referring to any object.” So now that we know a variable is just a little box o’ bits, we can get on with the work of changing those bits. We’ll look first at assigning values to primitives, and finish with assignments to reference variables.

Primitive Assignments

The equal (=) sign is used for assigning a value to a variable, and it’s cleverly named the assignment operator. There are actually 12 assignment operators, but the other 11 are all combinations of the equal sign and other arithmetic operators,

= *= /= %=
+= -= <<= >>=
>>>= &= ^= |=

Reference Variable Assignments

You can assign a newly created object to an object reference variable as follows:

Button b = new Button();

The preceding line does three key things:
■ Makes a reference variable named b, of type Button
■ Creates a new Button object on the heap
■ Assigns the newly created Button object to the reference variable b

You can also assign null to an object reference variable, which simply means the variable is not referring to any object:

Button c = null;

The preceding line creates space for the Button reference variable (the bit holder for a reference value), but doesn’t create an actual Button object.

public class Foo {
public void doFooStuff() {
}
}

public class Bar extends Foo {
public void doBarStuff() {
}
}
class Test {
public static void main (String [] args) {
Foo reallyABar = new Bar(); // Legal because Bar is a subclass of Foo
Bar reallyAFoo = new Foo(); // Illegal! Foo is not a subclass of Bar
}
}

Assigning One Reference Variable to Another

With primitive variables, an assignment of one variable to another means the contents (bit pattern) of one variable are copied into another. Object reference variables work exactly the same way. The contents of a reference variable are a bit pattern, so if you assign reference variable a to reference variable b, the bit pattern in a is copied and the new copy is placed into b. If we assign an existing instance of an object to a new reference variable, then two reference variables will hold the same bit pattern—a bit pattern referring to a specific object on the heap. Look at the following code:

import java.awt.Dimension;
class ReferenceTest {
public static void main (String [] args) {
Dimension a = new Dimension(5,10);
System.out.println("a.height = " + a.height);
Dimension b = a;
b.height = 30;
System.out.println("a.height = " + a.height +"after change to b");
}
}


In the preceding example, a Dimension object a is declared and initialized with a width of 5 and a height of 10. Next, Dimension b is declared, and assigned the value of a. At this point, both variables (a and b) hold identical values, because the contents of a were copied into b. There is still only one Dimension object—the one that both a and b refer to. Finally, the height property is changed using the b Java Operators reference.

Sunday, April 1, 2007

EJB stateless

Calculate.java


import javax.ejb.*;
import java.rmi.*;

public interface Calculate extends EJBObject{
public int multiply(int x, int y) throws RemoteException;
public float divide(int x, int y) throws RemoteException;
}

CalculateBean.java

import javax.ejb.*;
import java.rmi.*;

public class CalculateBean implements SessionBean {
SessionContext ctx;
public CalculateBean(){
System.out.println("Bean Instantiated");
}

public void setSessionContext(SessionContext context) {
this.ctx = context;
System.out.println(" This Context Associated with the ContainerContext ");
}

public void ejbCreate(){
System.out.println("ejbCreate");
}

public void ejbActivate(){}

public void ejbPassivate(){}

public void ejbRemove(){
System.out.println("ejbRemove");
}

public int multiply(int x, int y) {
System.out.println("multiply");
return x*y;

}

public float divide(int x, int y) {
System.out.println("divide");
float z= x/y;
return z;
}

}

CalculateClient.java

import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;

public class CalculateClient{
public static void main(String args[]){
try{
Context ctx = new InitialContext();
Object obj = ctx.lookup("app2JNDI");
CalculateHome home = (CalculateHome)PortableRemoteObject.narrow(obj,CalculateHome.class);
Calculate cal = home.create();
System.out.println("Create called");

int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int s=cal.multiply(x,y);
System.out.println("Product = " + s);

float t=cal.divide(x,y);
System.out.println("Quotient = " + t);


}catch(Exception e){
System.out.println(e);
}
}
}

CalculateHome.java

import javax.ejb.*;
import java.rmi.*;

public interface CalculateHome extends EJBHome{
Calculate create() throws CreateException, RemoteException;

}

Java Servlet Technology

Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. A servlet can almost be thought of as an applet that runs on the server side--without a face. Java servlets make many Web applications possible.

what is servlets?

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.

When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.


Servlet Life Cycle?

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

  1. If an instance of the servlet does not exist, the Web container
    1. Loads the servlet class.
    2. Creates an instance of the servlet class.
    3. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
  2. Invokes the service method, passing a request and response object. Service methods are discussed in the section Writing Service Methods.

If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. Finalization is discussed in Finalizing a Servlet.