With glassfish version 2.1 (Glassfish version 3.0 uses Eclipslink as persistence manager), I don't believe you can do this, because 2.1 uses Toplink as it's persistence manager, which I haven't be able to create dynamic connections with.
However if you add Eclipselink (eclipselink.jar) to your EAR file/project and use it as your Persistence Manager it should work. Add the jar to your project and add this following to your peristence.xml file....
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Check out the following to get Eclipselink...
http://www.eclipse.org/eclipselink/downloads/index.php#2.0.0
Then to create the link dynamically do the following....
Properties login = new Properties();
login.put(org.eclipse.persistence.config.PersistenceUnitProperties.JDBC_USER, username);
login.put(org.eclipse.persistence.config.PersistenceUnitProperties.JDBC_PASSWORD, password);
EntityManagerFactory emf_=Persistence.createEntityManagerFactory("persistence_unit",login);
EntityManager em = emf.createEntityManager();
[Message sent by forum member 'enderfake' (enderfake_at_yahoo.com)]
http://forums.java.net/jive/thread.jspa?messageID=378172