Sunday, March 15, 2009

The Essentials of Filters

Link: Complete Article From Sun Microsystems

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

Filters are important for a number of reasons. First, they provide the ability to encapsulate recurring tasks in reusable units. Organized developers are constantly on the lookout for ways to modularize their code. Modular code is more manageable and documentable, is easier to debug, and if done well, can be reused in another setting.

Second, filters can be used to transform the response from a servlet or a JSP page. A common task for the web application is to format data sent back to the client. Increasingly the clients require formats (for example, WML) other than just HTML. To accommodate these clients, there is usually a strong component of transformation or filtering in a fully featured web application. Many servlet and JSP containers have introduced proprietary filter mechanisms, resulting in a gain for the developer that deploys on that container, but reducing the reusability of such code. With the introduction of filters as part of the Java Servlet specification, developers now have the opportunity to write reusable transformation components that are portable across containers.

Others:
Link: O'Reilly Article on Basic of Filters

Thursday, March 12, 2009

Connecting To Sybase using JDBC

------------------CODE Snippets For Connecting to Sybase Database--------------------

Connecting to Sybase:

driverclassname = "com.sybase.jdbc2.jdbc.SybDriver";
dbprefix += "sybase:";

System.out.println("Loading jdbc driver: " + driverclassname);

Class.forName(driverclassname);

DBURL= "jdbc:sybase:::/"
DBURL = dbprefix + DBURL;

After that get the useid and password from user :

con = DriverManager.getConnection(DBURL, userid, password);

---------------------------------Connection Successful-------------------------------

Saturday, March 7, 2009

Servlet Application Event Listeners

Sun Microsystems released the proposed final draft of the Servlet 2.3 specification on October 20, 2000. Although the Servlet 2.3 specification is not in its final version yet, we do not have to wait any longer to play with the exciting new features defined by it. Apache's Tomcat has released a build, version 4.0 beta 1 that fully supports the Servlet 2.3 proposed final draft specification.

Link: Writing a Simple Listener
Link: Deploying Servlet Listeners

Code Snippets
Listing 1: An example of a very simple Servlet Event Listener(MyContextListener.java)


package com.listeners;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributesListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class MyContextListener
implements ServletContextListener {

private ServletContext context = null;
public MyContextListener() {}

//This method is invoked when the Web Application
//has been removed and is no longer able to accept
//requests

public void contextDestroyed(ServletContextEvent event)
{
//Output a simple message to the server's console
System.out.println("The Simple Web App. Has Been Removed");
this.context = null;
}
//This method is invoked when the Web Application
//is ready to service requests
public void contextInitialized(ServletContextEvent event)
{
this.context = event.getServletContext();
//Output a simple message to the server's console
System.out.println("The Simple Web App. Is Ready");
}
}



Link: Link to Complete Article

Sunday, March 1, 2009

Servlet Filters

Servlet filters are a new addition to the Servlet 2.3 specification that's in its public final draft stage, released in October, 2000. Filters are Java classes that can intercept requests from a client before they access a resource; manipulate requests from clients; intercept responses from resources before they are sent back to the client; and manipulate responses before they are sent to the client.

Link: Writing a Simple Filter
Link: Deploying Filters

Code Snippets
Listing 1: An example of a very simple filter (SimpleFilter.java)


package com.filters;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import java.io.IOException;
import javax.servlet.ServletException;

public class SimpleFilter implements Filter
{
private FilterConfig filterConfig;

public void doFilter (ServletRequest request,
ServletResponse response,
FilterChain chain)
{

try
{
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Request ...");

chain.doFilter (request, response);

System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Response ...");

} catch (IOException io) {
System.out.println ("IOException raised in SimpleFilter");
} catch (ServletException se) {
System.out.println ("ServletException raised in SimpleFilter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}



Link: Link to Complete Article

Others:

Link: Sun Microsystem Article on Basic of Filters