Monday, April 27, 2009

How to log records to log file? Using log4j

This piece of code will explain the usage of the log4j. In java development, you may have faced for error logging situation. Here is the solution.

Make a log configuration file with follwoing details in your project. Here log configuration file name is "logconfig.txt"






log4j.appender.debugl.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}|%m %n


There must be a file called "data_usage_log.txt" in C directory. Then it will upgrade with logs.

Add the following methods to your class constructors.

Logger preLogger



preLogger = Logger.getLogger("debugl");


you can add your error report as follows“Logging ”);

Sunday, April 19, 2009

Combine two pdf files using itext

Here is a simple way to combine two pdf files into one.
I have used iText-2.1.5.jar

This is the sample code


import com.lowagie.text.pdf.PdfCopyFields;

import com.lowagie.text.pdf.PdfReader;

public class PDFTest {

public static void main(String[] args) throws Exception {

System.out.println("Concatenate Two PDF");

PdfReader reader1 = new PdfReader("D:/upload/first.pdf");

PdfReader reader2 = new PdfReader("D:/upload/second.pdf");

PdfCopyFields copy = new PdfCopyFields(new FileOutputStream("D:/upload/concatenatedPDF.pdf"));






Thursday, April 16, 2009

Formatting text on Textarea/ Remove unwanted chars in Textarea

function escapeVal(textarea,replaceWith){

textarea.value = escape(textarea.value);

for(i=0; i<textarea.value.length; i++){

if(textarea.value.indexOf("%0D%0A") > -1){



else if(textarea.value.indexOf("%0A") > -1){



else if(textarea.value.indexOf("%0D") > -1){




return unescape(textarea.value);


If you need to remove un wanted chars in a textarea you can use the above java script.


set the textarea object as the 1st parameter and 2nd paramter is the replaceable char with unwanted ones.

Monday, April 6, 2009

Direct file access in weblogic server

In weblogic, you can keep a directory for all images,pdfs,....

how to do ...


you have to create a "weblogic.xml" file in WEB-INF in your application. This weblogic.xml should be like this.

<?xml version="1.0" encoding="UTF-8"?>







suppose you have a directory called as images in path /apps/test/images. You can give users to direct access those files as follows.


This is called as Virtual directory mapping in weblogic.

Connection pool/jndi

In normal java development, we use jdbc database connections. It takes several mili seconds to make connection between database server and your application.

In connection pool concept, there are several connections are built with the application server startup. This make web applications more fast.

As i mentioned, it makes several connection at the app sever startup. Developers can access those connections using jndi. There is a jndi for a conncetion pool. All the jndi are in the java jvm.

Developers has to look up for the jndi names and use the database connection.

Suppose your connection pool is "dbtest" . And you have mapped this pool with jndi name "jdbc/dbtest".

your lookup mathod will be like this in netbeans.

private DataSource getDbtest() throws NamingException {
Context c = new InitialContext();
return (DataSource) c.lookup("java:comp/env/dbtest");

The above method returns a sql data source. You can get a connection using it.