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.logger.debugl=INFO,debugl

log4j.appender.debugl.layout=org.apache.log4j.PatternLayout

log4j.appender.debugl=org.apache.log4j.DailyRollingFileAppender

log4j.appender.debugl.File=Log//data_usage_log.txt

log4j.appender.debugl.DatePattern='.'yyyy-MM

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


log4j.appender.debugl.File=c:/data_usage_log.txt

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

constructor(){    


PropertyConfigurator.configure("logconfig.txt");

preLogger = Logger.getLogger("debugl");

}


you can add your error report as follows
preLogger.info(“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 java.io.FileOutputStream;

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"));

copy.addDocument(reader1);

copy.addDocument(reader2);

copy.close();

}

}

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){

textarea.value=textarea.value.replace("%0D%0A",replaceWith)

}

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

textarea.value=textarea.value.replace("%0A",replaceWith)

}

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

textarea.value=textarea.value.replace("%0D",replaceWith)

}

}

return unescape(textarea.value);

}


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

parameters
----------

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"?>

<weblogic-web-app>

<virtual-directory-mapping>

<local-path>/apps/test</local-path>

<url-pattern>/images/*</url-pattern>

</virtual-directory-mapping>

</weblogic-web-app>



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

http://localhost/AppName/images/test.jpeg;


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.

jndi
------
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.