Tuesday, March 9, 2010

How to set datasource for Oracle in Jboss

A DataSource object is the representation of a data source in the Java
programming language. In basic terms, a data source is a facility for storing data.
It can be as sophisticated as a complex database for a large corporation or as
simple as a file with rows and columns. A data source can reside on a remote
server, or it can be on a local desktop machine. Applications access a data source
using a connection, and a DataSource object can be thought of as a factory for
connections to the particular data source that the DataSource instance represents.
The DataSource interface provides two methods for establishing a connection
with a data source.
Jboss provides a JNDI InitialContext implementation instance for each web
application running under it, in a manner that is compatible with those provided
by a Java2 Enterprise Edition application server. The J2EE standard provides a
standard set of elements in the /WEB-INF/web.xml file to reference resources;
resources referenced in these elements must be defined in an application-serverspecific
configuration.
In order to configure datasource for Oracle in Jboss I did the following steps:
1. Put Oracle's driver classes in the CLASSPATH. Copy Oracle's JDBC driver
.zip file /jdbc/lib/classes12.zip (in this case I put jdbc driver to lib) to the
server/default/lib directory.
2. Copy /docs/examples/jca/oracle-ds.xml , to /server/default/deploy dir.
3. Modify the oracle-ds.xml configuration file as per our connection
parameters like connection URL,username,password etc.
4. In web.xml making the following entry after tag.

DB Connection
jdbc/test
javax.sql.DataSource
Container

5. add a file jboss-web.xml under WEB-INF/ folder of the web-application.
This file should contain entry as:


DB Connection
jdbc/test
javax.sql.DataSource
java:/jdbc/test
Container


Notes: I am using Oracle 9i, jboss-4.2.3-GA, Jdk 1.5 for this configuration.
If we are not using Spring to lookup for jndi then use the following code to
look up:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/jdbc/test");
Connection conn = ds.getConnection();

Thursday, March 12, 2009

Changing Default character encoding scheme of Tomcat

Default URI encoding in Apache Tomcat is ISO-8859-1. There are many ways that we can change this encoding. i did this by writing my own filter. Code is as follows:

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter {

@SuppressWarnings("unused")
private FilterConfig filterConfig = null;
private String param;

public void destroy() {
this.filterConfig = null;
}

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

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

if (null != this.param) {
// instead of making these values hardcode we can supply these values from web.xml using init-param
response.setContentType("text/html; charset=ISO-8859-1");
request.setCharacterEncoding("ISO-8859-1");

chain.doFilter(request, response);

//do it again, since JSPs will set it to the default
response.setContentType("text/html; charset=ISO-8859-1");
request.setCharacterEncoding("ISO-8859-1");
}
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}