Saturday, February 21, 2009

Creating a Date object in java

Creating instances of java.util.Date & java.sql.Date classes
using a Calendar Class in java.


Java Code:

import java.util.Calendar;

public class DateUtil
{
public void createDates()
{
int year = 2006;
int month = 0; //January
int date = 1;
Calendar cal = Calendar.getInstance();
//Clear all fields
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);

//Create instance of java.util.Date
java.util.Date utilDate = cal.getTime();

//Create instance of java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());

System.out.println(utilDate);
System.out.println(sqlDate);
}

public static void main(String[] args) {
DateUtil dateutil = new DateUtil();
dateutil.createDates();
}
}

Read more...

O'Reilly - Java Web Services eBook

O'Reilly - Java Web Services:

Java Web Services explains how to use SOAP to perform remote method calls and message passing and how to use WSDL to describe the interface to a web service or understand the interface of other's service. Using UDDI to advertise (publish) and look up services in each local or global registry.

Java Web Services also discusses security issues, interoperability issues, integration with other Java enterprise technologies like EJB, the work being done on the JAXM and JAX-RPC packages, and integration with Microsoft's .NET services.

Download Java Web Services eBook

Read more...

Professional Java Development with the Spring Framework

Professional Java Development with the Spring Framework :

The Spring Framework is a major open source application development framework that makes Java/J2EE(TM) development easier and more productive. This book shows you not only what Spring can do but why, explaining its functionality and motivation to help you use all parts of the framework to develop successful applications.

Wrox Professional guides are planned & written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.

Key learnings from this book:

  • Core Inversion of Control container
  • Concept of Dependency Injection
  • Spring’s Aspect Oriented Programming (AOP) framework
  • Spring’s programmatic & declarative transaction management
  • Spring services for accessing and implementing EJBs
  • Spring’s remoting framework
Download Professional Java Development with the Spring Framework eBook

Read more...

Sams - BEA WebLogic Server 8.1 Unleashed

Sams - BEA WebLogic Server 8.1 eBook:

BEA positioned WebLogic Server as the premier J2EE Web Services development platform With the release of WebLogic Server 7.0 in June 2002. WLS integrated WebLogic Workshop and other key tools to provide developers with a stable, market-leading product designed for the next generation of Java applications based on Web Services on the latest J2EE platform With the next release in late 2002.

BEA WebLogic Server is the leading J2EE application server, holding almost 40% of the market share in this competitive category. WebLogic Server Unleashed is designed to be the definitive reference work for the WLS developer, offering an in-depth look at the capabilities provided by WLS 7.X and illustrating the best development practices.


Download BEA WebLogic Server 8.1 eBook

Read more...

Creating a zip file in Java

Java Code Example: Create a zip file in Java

Steps:

  • Create an input stream from the file to compress
  • Read from it we write the contents to an output stream.
  • Output stream is of type ZipOutputStream which takes an FileOutputStream as parameter.
  • Add a zip entry to the output stream before we start writing to it.
  • Clean up by closing the zip entry and both the input stream and output stream.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
*
* @author javadb.com
*/

public class Main {

/**
* Creates a zip file
*/

public void createZipFile() {

try {
String inputFileName = "test.txt";
String zipFileName = "compressed.zip";

//Create input and output streams
FileInputStream inStream = new FileInputStream(inputFileName);
ZipOutputStream outStream = new ZipOutputStream(new
FileOutputStream(zipFileName));

// Add a zip entry to the output stream
outStream.putNextEntry(new ZipEntry(inputFileName));

byte[] buffer = new byte[1024];
int bytesRead;

//Each chunk of data read from the input stream
//is written to the output stream
while ((bytesRead = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, bytesRead);
}

//Close zip entry and file streams
outStream.closeEntry();

outStream.close();
inStream.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
new Main().createZipFile();
}
}

Read more...

Saturday, February 14, 2009

Thread Constructors

Several constructors are available for creating new Thread instances.
  • Thread()
  • Thread(String)
  • Thread(Runnable)
  • Thread(Runnable,String)
  • Thread(ThreadGroup,String)
  • Thread(ThreadGroup,Runnable)
  • Thread(ThreadGroup,Runnable,String)
  • Thread(ThreadGroup, Runnable, String, long)
ThreadGroup– All threads belongs to an instance of the ThreadGroup Class. ThreadGroup is used to represent a group of threads. ThreadGroups can be shown in a hierarchical manner. There is only one root ThreadGroup that contains all other thread and groups and each subgroups can contain other groups and threads.

All thread have only one thread group. And all thread groups (except the root thread group) belongs to exactly one parent thread group. Threads can access only belonging thread group.

When a new ThreadGroup is created, it is added as a member of existing ThreadGroup.
If a thread x in group1, and executes the code:
  • ThreadGroup group2=new ThreadGroup(“group2”);
Then the newly formed group2 comes under group1. If you want a parent group other than default then you have to specify the parent group at the time of creation.
  • ThreadGroup group2=new ThreadGroup(group2,“group3”);
Then newly formed group3 comes under the group2.

Some important methods are:

getName() – This method is used to retrieve the name of particular group.
  • ThreadGroup g=new ThreadGroup(“RoseIndia”);
  • String gname=g.getName();
getParent() – This method is used to retrieve the name of parent threadgroup of sub group.
  • ThreadGroup group=group3.getParent();
activeGroupCount() – This method returns the number of active thread group in a particular thread group and all its subgroups.
  • int size=group.activeGroupCount();
getThreadGroup() – This method is used to know the thread is belong to which thread group.
  • ThreadGroup group=threadx.getThreadGroup();

Read more...

Difference between Application Server & Web Server

Application server provides methods that client applications can call. A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.

Application server:

Application server, according to our definition, an application server exposes business logic to client applications through various protocols, possibly including HTTP. While a Web server mainly deals with sending HTML for display in a Web browser, an application server provides access to business logic for use by client application programs. The application program can use this logic just as it would call a method on an object (or a function in the procedural world).

Such application server clients can include GUIs (graphical user interface) running on a PC, a Web server, or even other application servers. The information traveling back and forth between an application server and its client is not restricted to simple display markup. Instead, the information is program logic. Since the logic takes the form of data and method calls and not static HTML, the client can employ the exposed business logic however it wants.

In most cases, the server exposes this business logic through a component API, such as the EJB (Enterprise JavaBean) component model found on J2EE (Java 2 Platform, Enterprise Edition) application servers. Moreover, the application server manages its own resources. Such gate-keeping duties include security, transaction processing, resource pooling, and messaging. Like a Web server, an application server may also employ various scalability and fault-tolerance techniques.

Web server:

A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser.

Understand that a Web server's delegation model is fairly simple. When a request comes into the Web server, the Web server simply passes the request to the program best able to handle it. The Web server doesn't provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses. The server-side program usually provides for itself such functions as transaction processing, database connectivity, and messaging.

While a Web server may not itself support transactions or database connection pooling, it may employ various strategies for fault tolerance and scalability such as load balancing, caching, and clustering—features oftentimes erroneously assigned as features reserved only for application servers.

Read more...

Servlet 3.0 New Features

Web framework pluggability

Almost all of the Java based web frameworks build on top of servlets. Most web frameworks today plugin either through servlets or through web.xml. Annotations to define some of the servlets, listeners, filters will help in making this possible. Programatic access to web.xml and dynamic changes to configuration of a webapp are desired features. This JSR will aim to provide the ability to seamlessly plugin different web frameworks into web appplications.

Security

  • Ability to login / logout.
  • Self registration.
Annotations

use of annotations for the declarative style of programming. As part of the EoD effort the goal is to have zero configuration for web applications. The deployment descriptors would be used to override configuration.

Generics

Use generics in the API where possible. Use of other language enhancements where possible to improve the usability of the API.

Async and Comet support

Non-blocking input - The ability to receive data from a client without blocking if the data is slow arriving.

Non-blocking output - The ability to send data to a client without blocking if the client or network is slow.

Delay request handling - The comet style of Ajax web application can require that a request handling is delayed until either a timeout or an event has occurred. Delaying request handling is also useful if a remote/slow resource must be obtained before servicing the request or if access to a specific resource needs to be throttled to prevent too many simultaneous accesses.

Delay response close - The comet style of Ajax web application can require that a response is held open to allow additional data to be sent when asynchronous events occur.

Blocking - Non-blocking notification - The ability to notify push blocking or non-blocking events. Channels concept - The ability to subscribe to a channel and get asyncronous events from that channel. This implies being able to create, subscribe, unsubscribe and also apply some security restriction on who can join and who cannot.

Other Features:
  • Better welcome file support.
  • ServletContextListener ordering.
  • Container wide definition for init params.
  • File upload - progress listener - where to store interim and final file.
  • Clarifications of thread-safety issues.

Read more...

JavaBeans

JavaBeans is a new component architecture for Sun's Java language. Like the language from which JavaBeans draws its name, it is portable across many platforms; any environment supporting a JDK 1.1 interpreter will be capable of using JavaBeans. JavaBeans offers the ability to write applications quickly and easily, by using a palette of components that can be assembled to form a larger application. User-interface components, such as trees, lists, or graphical buttons can make applications come alive, without the need for writing custom components for each and every application. Networking protocols can be encapsulated in a component, allowing developers to simply plug in email and web support into their applications. Literally any component you can imagine can be written as a JavaBean, and then plugged into an application.

Features:
  • JavaBeans support properties, allowing an application to read and modify their values.
  • JavaBeans support events, allowing vendors to create their own unique events.
  • JavaBeans support the BeanInfo interface, allowing vendors to specify exactly which properties and methods are available, and icons for beans which can be displayed on a toolbar.
  • JavaBeans are highly configurable, and the state of a bean can be saved and restored through 'serialization'.
JavaBeans have a significant competitive advantage over ActiveX - JavaBeans can be instantly converted into ActiveX controls via the bridge, but ActiveX controls cannot be easily converted into JavaBeans. JavaBeans also offer the security and robustness that developers have come to know and love, whereas ActiveX controls remain dangerous (despite the innovation of digital signatures), as they have low level access to features of the operating system. JavaBeans used in applets are bound by the same restrictions (file and network access) as their applet hosts, yet ActiveX controls that are signed have a much larger potential to wreak havoc on the system. Coupled with their lack of portability, ActiveX components have a smaller target audience, and there is a perception that they can be dangerous when executed indiscriminately from the web.

For the serious component vendor, or application developer, JavaBeans is an important new technology that must be learnt to stay ahead. ActiveX has its place, for now, but with the introduction of the JavaBeans Bridge, it would be prudent to learn the JavaBeans architecture while it remains in its infancy.

Read more...

Java Virtual Machine (JVM)

Java is that it uses bytecode - a special type of machine code. Java bytecode executes on a special type of microprocessor. Strangely enough, there wasn't a hardware implementation of this microprocessor available when Java was first released. Instead, the processor architecture is emulated by what is known as a "virtual machine". This virtual machine is an emulation of a real Java processor - a machine within a machine (Figure One). The only difference is that the virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

The Java Virtual Machine is responsible for interpreting Java bytecode, and translating this into actions or operating system calls. For example, a request to establish a socket connection to a remote machine will involve an operating system call. Different operating systems handle sockets in different ways - but the programmer doesn't need to worry about such details. It is the responsibility of the JVM to handle these translations, so that the operating system and CPU architecture on which Java software is running is completely irrelevant to the developer.

Different JVM implementations:

Though implementations of Java Virtual Machines are designed to be compatible, no two JVMs are exactly alike. For example, garbage collection algorithms vary between one JVM and another, so it becomes impossible to know exactly when memory will be reclaimed. The thread scheduling algorithms are different between one JVM and another (based in part on the underlying operating system), so that it is impossible to accurately predict when one thread will be executed over another.

Initially, this is a cause for concern from programmers new to the Java language. However, it actually has very little practical bearing on Java development. Such predictions are often dangerous to make, as thread scheduling and memory usage will vary between different hardware environments anyway. The power of Java comes from not being specific about the operating system and CPU architecture - to do so reduces the portability of software.

The Java Virtual Machine provides a platform-independent way of executing code, by abstracting the differences between operating systems and CPU architectures. Java Runtime Environments are available for a wide variety of hardware and software combinations, making Java a very portable language. Programmers can concentrate on writing software, without having to be concerned with how or where it will run. The idea of virtual machines is nothing new, but Java is the most widely used virtual machine used today. Thanks to the JVM, the dream of Write Once-Run Anywhere (WORA) software has become a reality.

The Java Virtual Machine forms part of a large system, the Java Runtime Environment (JRE). Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM. The portability of Java comes from implementations on a variety of CPUs and architectures. Without an available JRE for a given environment, it is impossible to run Java software.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP