jsp - java connection class, passing string from servlet -


i developing web application using jsp , servlets.(database: oracle10, container: glassfish).

i have developed class creating connection.

(conn.java):

public class conn {     private connection con = null;     public connection getcon()     {         string home = system.getproperty("user.home");         home  = home+"\\dbfile.properties";         //read properties of connection string file , create connection         return con;     } } 

then have 4 other classes select, insert, update, delete transactions using above conn.java class getting connection:

(select.java)

public class select {     private conn connection = new conn();     private connection con = null;     private preparedstatement pstmt = null;     private resultset rs=null;      public string[][] selectdata(string query)     {         string[][] data=null;         if(con==null)         {             con = connection.getcon();         }         //execute query put data in 2 dimensional array , return         return data;     } } 

insert, update , delete coded similar way above select.java coded.

so in servlets using 4(select, insert, update, delete) classes, passing query them , getting result.

sample servlet

public class sampleserv extends httpservlet  {     select select = new select();     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception      {         string data[][];         data = select.selectdata(query_goes_here);         //and other code     } } 

it works perfectly, our requirement change database string after user login. want pass user specific property file name conn.java class. storing property file name in cookie.

i have think 1 way of doing this:

  1. get cookie value in servlets
  2. pass cookie value selectdata() method of select.java class , class
  3. pass cookie value getconn() method of conn.java class

so want know if there better way pass connection string file name conn.java class?

thanks in advance.

httpsession user info should stored (with concerns).

in case, seem have many different web applications, each of them have different session, , need update of them.

i prefer approach (and personal opinion, can discussed) based in threadlocal class. can write servlet filter,

  • read cookie value
  • store in threadlocal
  • after filter.dofilter method, have clean (this extremely important, don't have chance of mixing sessions), put clean method in block gets executed whatever happens.

the main advantage of approach may not have access httpsession or httpservletrequest, , still able value in threadlocal.

an example of threadlocal container can use 1 :

public class threadlocalcontainer {    private static threadlocal<string> userid=new threadlocal<string>();    public static string getuserid(){        return userid.get();    }    public static void setuserid(string uid){         userid.set(uid);    }    public static void resetuserid(){         userid.remove();    } } 

then able access userid calling threadlocalcontainer.getuserid() everywhere in code, if don¡t have access http context.

make sure define servlet filter in webapps, userid gets set.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -