Monday, September 24, 2007

MISC (Shell scripts, Java)

Shell script to remove ^M charactors from a file ftped through pc.

sed 's/^M//;s/^Z//' $1 > $1.new

To type ^M, manually enter with (ctrl+v, ctrl+m). Same for ^Z.

Customize SQLExeption's e.getMessage()

catch(SQLException e)
{
if(e.getErrorCode()==1 && e.getMessage().contains("unique constraint")) {
SQLException se=new SQLException("YOUR CUSTOMIZED MESSAGE");
se.setStackTrace(e.getStackTrace());
throw se;
}
else throw e;
}



Connection Pooling configuration and sample usage using JNDI-DBCP

context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/appname" docBase="appname">
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" username="user" password="pass" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@ipaddr:1521:sid" maxActive="8" maxIdle="4" />
</Context>


web.xml :

<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

Code sample :

Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();

Also, some useful parameters you can use to avoid the resource leaks due to connection pooling
removeAbandoned="true"
removeAbandonedTimeout="600"
logAbandoned="true"
The above can be configured in context.xml along with maxActive and maxIdle parameters.

Refer to following links for detail explanations:
http://commons.apache.org/dbcp/
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html
http://commons.apache.org/dbcp/configuration.html

Tuesday, September 4, 2007

Web Application Development

Creating a simple web application using maven, eclipse and apache-tomcat

Pre-requisites:

1. Download and install maven 2
2. Download and install eclipse
3. Install maven 2 eclipse plugin from http://m2eclipse.codehaus.org/ update site from eclipse. Also, eclipse needs to know where maven repository is, which can be set using following command:
mvn -Declipse.workspace= eclipse:add-maven-repo 

3. Download and install apache tomcat 5.5.23 (make sure you have jdk 1.5 or higher installed before installing tomcat)

Steps :
1. Now, go to eclipse-workspace directory and create a web application maven project using following command.

mvn archetype:create -DgroupId=org.test.webapp -DartifactId=HelloWorldWebApp -Dpackagename=org.test.webapp -DarchetypeArtifactId=maven-archetype-webapp

2. Go to HelloWorldWebApp directory
-open pom.xml add any necessary dependencies. for e.g
<dependency>
<groupid>jdbc</groupid>
<artifactid>oracle</artifactid>
<version>1.4</version>
</dependency>

- run following commands from command line to create an eclipse project :
mvn install
mvn eclipse:eclipse

3. Start a eclipse project with HelloWorldWebApp and now, you are ready to go.
You may add following in your .classpath file
<classpathentry kind="src" path="src/main/java">

4. Create a new class name it HelloWorldServlet:

Write your code :

package org.test.webapp;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class HelloWorldServlet extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{

res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.write("Hello World:doPost");
out.flush();
out.close();
return;
}

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{

res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.write("Hello World:doGet");
out.flush();
out.close();
return;
}
}
5.Modify wrc\main\webapp\WEB-INF\web.xml
For e.g :

<web-app>
<display-name>Hello World </display-name>
<description>
Provides Portlet Application
</description>

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.test.webapp.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>


6. To deploy :
compile using maven :
mvn clean compile war:war

deploy on tomcat
start tomcat
open a browser point to localhost:8080/manager/html
deploy the war file
try in your browser url : http://localhost:8080/hello