Wednesday, May 14, 2008

Java and MS-Office

Just in case ever need to
  1. Read a Microsoft word document in Java
  2. Import Excel in JTable
In Conference Java One 2008 , Oracle also gave demo on how to integrate MS-Excel with ADF

Java Strings Immutable

Many modern languages (like java) employ “the immutable object” paradigm, which solves a lot of problems including the
  1. Memory conservation
  2. Addressing concurrency concerns
  3. Cache localization
Memory Conservation - A pool of string objects ( constant ) is maintained with references pointing to the constant objects. Instead of each variable having new String object. Re usability leads to lesser memory usage.

Concurrency Concerns - if you change any string need to consider that more than one references may be pointing to same string , so if you will change that string all references must also be updated ( thats tough ) so a new object for changed string will be created and only that particular reference will be point to that new object hence actual object will never change.

I am not sure about cache localization ??

Linux : how to list directory only

To see a listing of all directories in current directory

  • ls -l | grep ^d ( this will show names with details )
  • ls -d */ ( will how names only )

If you want to see hidden directory also
  • ls -d .*"/" *"/"
  • find . -type d -exec ls -d {} \;
  • find . -type d

Tuesday, May 13, 2008

JavaScript : String Performance

Just wanted to book mark these two "excellent" blog entries ...
  1. http://ajaxian.com/archives/everything-you-wanted-to-know-about-string-performance
  2. http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/

Servlets and Garbage Collection

Basic Cycle
  1. The destroy() method is called by the servlet container when the container decides to unload the servlet instance itself.
  2. After the destroy() method completes, the servlet engine unloads the servlet,.
  3. Then, the JVM eventually performs garbage collection on the memory resources associated with that servlet object.
Effecient Servlet : Goals

When can you say that a Servlet is efficient , well of course when it does your work in most effecient manner or in other words the algorithm/logic that you have in it is effecient... but there's more to its efficiency .. Read On !!

Secondly even for High volume Web applications, one needs to carefully control the amount of garbage created during page generation. More the garbage created, fewer the pages generated, and the slower is the response time for a particular request. ( This bottleneck occurs because the Java VM has to steal cycles to run garbage collection when it should be generating pages. ) On the flip side , if too much garbage is created, request processing could halt entirely. Which is unacceptable.


Third is to keep the browser active: not too much of "waiting for response" messages. To put it in programatic terms the time a bean spends querying backend is the time a user spends waiting for page to display. This can -- and should -- be avoided using servlet response streaming techniques.

All About Servlets : Run The Servlet


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class AllAboutServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public AllAboutServlet() {
System.out.println("CTOR : Servlet constructor can be overriden .. " +
"should not be private , protected , nor default");
System.out.println("CTOR : The ctor will be called by the container ... ");
System.out.println("CTOR : At present its a normal java object .. " +
"doesnt understand ServletContext and ServletConfig ");
System.out.println("CTOR : Servlets are usually instantiated by the servlet " +
"container using the Class.newInstance() method, with no arguments");
}

private AllAboutServlet(int i){
System.out.println("Parameterized ctor value "+i);
System.out.println("It is possible to have a custom constructor for a servlet, " +
"so long as you also add a default constructor with no arguments ?? WHY ??");
System.out.println("the servlet gets instantiated by the container dynamically");
System.out.println("java does not allow to call parametrized constuctor for dynamically " +
"loaded clases");
}

public static void static_method(){
System.out.println("Can have Static method in servlet .. who can call it ??");
System.out.println("Probably no one is stopping other classes to use servlet to" +
"instantiate amnd use it as a normal object ");
System.out.println("Other classes may even call init() service() and then destroy() " +
"that wont make it servlet.. unless container call");
System.out.println("Only container can use servlet class as a servlet ");
}

public void init(ServletConfig config) throws ServletException {
System.out.println("INIT : Servlet init called .. now java object turns servlet .. " +
"understands ServletContext and ServletConfig");
System.out.println("INIT : perform one-time expensive operations, such as acquiring " +
"thread-safe resource acquisition mechanisms ");
// One effective use of the Servlet init method is the creation and caching of
// thread-safe resource acquisition mechanisms, such as JDBC DataSources, EJB Homes,
// and Web Services SOAP Mapping Registry. use this for caching data ...
super.init(config);
// COMPULSORY the abstract superclass implementation of the method in GenericServlet
// stores the servlet configuration object.

// If an exception is thrown in your init(ServletConfig) method, it may be appropriate
// to use the destroy() method to help handle the case. If so, you should still throw
// a ServletException to ensure the servlet is not put into service in this state.
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>AllAboutServlet</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
out.close();

System.out.println("DOGET : doGet called ");
System.out.println("DOGET : one may call delete() from inside service() ... It will do whatever logic you have in destroy() (cleanup, remove attributes, etc.) but it won't \"unload\" the servlet instance itself");
System.out.println("");
}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>AllAboutServlet</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a POST. This is the reply.</p>");
out.println("</body></html>");
out.close();
}

public void doPut(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
}

public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
}

public void destroy(){
System.out.println("DESTROY : destroy called by contianer .. when is it called ??" +
"When your application is stopped or Servlet Container shuts down ");
System.out.println("DESTROY : Not when embedded oc4j stopped ");
System.out.println("DESTROY : destroy() does not imply garbage collection");
System.out.println("DESTROY : write the destroy() method and destroy your servlet" +
"because if many servlets running on the same webserver are not destroyed explicitly," +
"then the performance of the web server falls down");
System.out.println("DESTROY : destroy() is not guaranteed to be called... but if it is, " +
"you should make sure it works right");
}

public void destroy(int i){
System.out.println("may overload .. its syntactically ok ... container wont call this...");
}

public void point1() {
System.out.println("Is it possible that the servlet container will call destroy()" +
"while the servlet is still processing a request inside service() on a different thread ?");
System.out.println("yes, but it's a degenerate case. The servlet container is required" +
"to wait a reasonable amount of time for current requests to finish processing. So if your" +
"request is not totally lagging, it should work out fine. If you're spending more than" +
"a few seconds processing a request, that's a UI problem that you should try to work out first.");
}
public void point2() {
// GenericServlet has methods ... init() destroy() log() getInitParameter() getInitParameterNames()
// getServletContext() getServletConfig() getServletInfo() getServletName()
}
public void point3() {
// HttpServlet is declared abstract because the default implementations of the main service methods
// do nothing and must be overridden
}
public void point4() {
// The HttpServletRequestWrapper and HttpServletResponseWrapper classes are designed to make it
// easy for developers to create custom implementations of the servlet request and response types.
// The classes are constructed with the standard HttpServletRequest and HttpServletResponse instances
// respectively and their default behaviour is to pass all method calls directly to the underlying
// objects.
}

}

All About Servlets : Introduction

The Servlet Philosophy
To write a server side program , need two things
  1. Program at socket level .. handle all the incoming requests
  2. Program at logical level .. write the correct response to each request
The Servlet API is based on simple philosophy , letting the programmers concentrate on the second option and taking care of first option by themselves.

Load On Startup
The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this element must be an integer indicating the order in which the servlet should be loaded.
  1. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses.
  2. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-startup value.
When is destroy( ) called ?
I could think of following cases...
  1. When your application is stopped
  2. Servlet Container shuts down
  3. In init( ) method if an exception is thrown
What if i call the destroy( ) explicitly
N
othing , it will just execute like a normal java method call it wont unload the servlet. The unloading happens only if the Container calls the destroy( ).

From the Spec


The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.

When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down.

Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class.

After the destroy method completes, the servlet container must release the servlet instance so that it is eligible for garbage collection.


Can I use a constructor instead of init(ServletConfig) ?
Servlet constructor can be overriden .. not be private , protected , nor default The constructor will be called by the container. In the constructor it doesn't understand classes like ServletContext and ServletConfig.

It is possible to have a custom constructor for a servlet, so long as you also add a default constructor with no arguments the servlet gets instantiated by the container dynamically java does not allow to call parametrized constuctor for dynamically loaded clases

It is possible to have a custom constructor for a servlet, so long as you also add a default constructor with no arguments, but constructors are not called in the standard servlet lifecycle. Servlets are usually instantiated by the servlet container using the Class.newInstance() method, with no arguments. At this point, the servlet has no reference to its configuration or the general servlet context, so it cannot do any useful start-up activity. These configuration references are only available through the init(ServletConfig) method.

Why is it necessary to call super.init(config)?

Programmers are expected to call the super.init(ServletConfig) in the init(ServletConfig) method so that the abstract superclass implementation of the method in GenericServlet stores the servlet configuration object. When the configuration object is stored in this way, the servlet methods getInitParameter(String) and getInitParameterNames() can be called instead of calling the equivalent methods on the configuration object.

Should I get my database connection in the init() method?

The init(ServletConfig) method is only called once when a servlet is brought into service by the servlet container, not for each new servlet thread. If you create a single database connection in the init(ServletConfig) method and use it to all handle servlet requests, you must ensure all operations are synchronized or you will get unpredictable results. You must also ensure the connection is closed when the servlet is taken out of service by overriding the servlet's destroy() method.

Generally it is better to use the init(ServletConfig) method to register the database driver and get a reference to the DriverManager, a DataSource or database connection pool. Then use the connection provider to get a connection for each request within a try/catch block. This way you can handle the case where the connection fails, and ensure that all connections are closed in any case. The close() method of a pooled Connection instance just returns it to the pool.

What's the difference between ServletConfig and ServletContext?

The ServletContext object - represents the context for the whole Web application in which a servlet is deployed, and contains initialisation parameters that are shared amongst all servlets in the application.
The ServletConfig object - represents the configuration for a single specific servlet.

How can I load a Properties file for my servlet?

There are standard methods for identifying and loading this type of static property set into servlets via the ServletContext object. The primary method is getResourceAsStream(String path), which opens an InputStream to the path given in the argument. The path must begin with a forward slash, /, and is relative to the context root, so for instance you might use the path:
/WEB-INF/servlet.properties

Is it possible to overload the destroy() method?

Any Java method may be overloaded, but the overloaded version will not be called directly by a servlet container's lifecycle methods. The servlet container expects all servlets to have a standard no argument destroy() method, so your servlet is safe to use destroy methods with other signatures.
    public void destroy(final String arg) {
// Overloaded method body
}

Can I call destroy() from the service() method ?

The destroy() method has a special significance in the servlet lifecycle when it is called by the servlet container, but is a normal method in all other respects. If you have a typical destroy() method that does servlet clean-up work before the servlet is taken out of service, it is difficult to imagine why you would want to call it from the service() method, though it is possible. Normally, you should handle service requests by overriding the relevant doGet() or doPost() method.

What happens if I call destroy() from init() ?

If the destroy() method is called from the init(ServletConfig) method, whatever statements are contained in the method will be executed and the method will return, it will not affect the lifecycle status of the servlet from the container's point of view. If the init(ServletConfig) method returns without exception, the servlet container will put the servlet into service regardless.

If an exception is thrown in your init(ServletConfig) method, it may be appropriate to use the destroy() method to help handle the case. If so, you should still throw a ServletException to ensure the servlet is not put into service in this state.

Can have Static method in Servlet .. If yes , who can call it ??
Probably no one is stopping other classes to use servlet to instantiate and use it as a normal object Other classes may even call init() service() and then destroy() that wont make a java object a servlet.. unless container calls the same methods Only container can use java class as a servlet.

Summary
To sum up basically a Servlet first becomes a Java object and then a Servlet. I mean a container will first make a Java Object then convert it to Servlet Instance.

You can very well use this as a java object ( in case you call destroy( ) from init( ) etc) but it wont effect the lifeCycle of a servlet. Life cycle of servlet is decided and executed by container servlets cant manage / change their own life cycle by calling there methods.