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 > $

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

web.xml :

<description>Oracle Datasource example</description>

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
The above can be configured in context.xml along with maxActive and maxIdle parameters.

Refer to following links for detail explanations:

Tuesday, September 4, 2007

Web Application Development

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


1. Download and install maven 2
2. Download and install eclipse
3. Install maven 2 eclipse plugin from 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

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

PrintWriter out = res.getWriter();
out.write("Hello World:doPost");

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

PrintWriter out = res.getWriter();
out.write("Hello World:doGet");
5.Modify wrc\main\webapp\WEB-INF\web.xml
For e.g :

<display-name>Hello World </display-name>
Provides Portlet Application



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