[Tips] #1 – Firefox, Cloud Storage, Eclipse and SVN

1. We can easily view a list of “about” pages of Firefox browser by entering “about:about” on address bar. (Google Chrome also)

2. I usually backup my data in “cloud” as Google Drive, Drop box and Ubuntu One. We install and use them on local computer conveniently!

3. I have a problem with workspace of Eclipse, I try to delete my workspace from external folder (for example, the folder is named “myworkspace” ) but I restart Eclipse the folder also is created automatically. We can solve problem by setting shutdown workspace on Eclipse.

Window > Preferences > General > Startup and Shutdown. Remove it!

4. It is nice way by using Google Code to storage our code. With my google account, I set up a SVN server to manage code of my projects.

See video tutorial here: http://www.youtube.com/watch?v=FwGJZcCj1Yo


Redirect HTTP to HTTPS using Servlet Filter

1. Write a filter to redirect from http to https

package vn.nvanhuong.servlet;

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 HTTPSFilter implements Filter {

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

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

String uri = req.getRequestURI();
String getProtocol = req.getScheme();
String getDomain = req.getServerName();
String getPort = Integer.toString(req.getServerPort());

if (getProtocol.toLowerCase().equals("http")) {

// Set response content type

// New location to be redirected
String httpsPath = "https" + "://" + getDomain + ":" + getPort
+ uri;

String site = new String(httpsPath);
res.setHeader("Location", site);

// Pass request back down the filter chain
chain.doFilter(req, res);


public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub


public void destroy() {
// TODO Auto-generated method stub



2. Compile above filter and create following entries in web.xml

<!-- HTTPS Redirect -->

[1]. http://www.tutorialspoint.com/servlets/servlets-page-redirect.htm
[2]. http://www.easywayserver.com/blog/jsp-get-url/

[Java] Compare two list


I have two classes are Employee and Department

class Department{
private Long deptId;
private String deptName;

public Long getDeptId() {
return deptId;
public void setDeptId(Long deptId) {
this.deptId = deptId;
public String getDeptName() {
return deptName;
public void setDeptName(String deptName) {
this.deptName = deptName;

class Employee{
private Long empId;
private String empName;
private Department dept;

public Employee() {

public Employee(Long empId, String empName, Department dept) {
this.empId = empId;
this.empName = empName;
this.dept = dept;
public Long getEmpId() {
return empId;
public void setEmpId(Long empId) {
this.empId = empId;
public String getEmpName() {
return empName;
public void setEmpName(String empName) {
this.empName = empName;
public Department getDept() {
return dept;
public void setDept(Department dept) {
this.dept = dept;


And, I write  a class for compare two list with data from Employee class: ListComparisonTest

package tdd.sprint10;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.*;

public class ListComparisonTest {

private Department dept;

public ListComparisonTest() {

public void setUp() throws Exception {
dept=new Department();

public void tearDown() throws Exception {

public void testCompareList() {
Employee emp1 = new Employee();
emp1.setEmpName(&quot;Van Huong&quot;);

Employee emp2 = new Employee();
emp2.setEmpName(&quot;Xuan Vinh&quot;);

List&lt;Employee&gt; firstList = new ArrayList&lt;Employee&gt;();

List&lt;Employee&gt; secondList = new ArrayList&lt;Employee&gt;();
secondList.add(new Employee(1L,&quot;Van Huong&quot;,dept));
secondList.add(new Employee(2L,&quot;Xuan Vinh&quot;,dept));

assertEquals(firstList.size(), secondList.size());


It’s fail when I  run the test class with JUnit Test!

The solution: create hashCode() and equals() functions on Employee class

On Eclipse IDE, press Alt + Shift + S, chose  generate and hashCode() and equals() functions.

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((empId == null) ? 0 : empId.hashCode());
result = prime * result + ((empName == null) ? 0 : empName.hashCode());
return result;

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
return true;

[Spring + Hibernate] Configure connection pool with Spring based – c3p0, Apache DBCP and BoneCP

Application Context

1. Without connection pool

<!-- Data Source Declaration -->
<bean id="myDataSource"
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />

2. c3p0

<!-- C3P0 Connection Pool -->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />

<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />

3. Apache DBCP

<!-- DBCP configuration -->
<bean id="dbcp" class="com.jolbox.bonecp.BoneCPDataSource"
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="removeAbandoned" value="true" />
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />

4. BoneCP

<!-- BoneCP configuration -->
<bean id="boneCP" class="org.apache.commons.dbcp.BasicDataSource"
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="60" />
<property name="idleMaxAgeInMinutes" value="240" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />

and session factory declaration…

<!-- Session Factory Declaration -->
<bean id="mySessionFactory"
<property name="dataSource" ref="myDataSource" /> <!-- without connection pool -->
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.default_schema">myagile</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>

[1]. http://jolbox.com/
[2]. http://www.javacodegeeks.com/2012/04/jsf-2-primefaces-3-spring-3-hibernate-4.html
[3]. http://www.mchange.com/projects/c3p0/
[4]. http://www.mkyong.com/hibernate/how-to-configure-the-c3p0-connection-pool-in-hibernate/