1

I am developing a web application using JSP and Servlets.(Database: Oracle10, Container: Glassfish).

I have developed A Class for 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 from that file and Create Connection
        return con;
    }
}

Then I have a 4 other classes for SELECT, INSERT, UPDATE, DELETE transactions which are using above Conn.java class for 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 two dimensional array and then return it
        return data;
    }
}

INSERT, UPDATE and DELETE are coded similar way as above Select.java is coded.

So in all servlets I am just using those 4(SELECT, INSERT, UPDATE, DELETE) classes, passing query to them and getting the 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, but now our requirement is to change Database String after user is login. So I want to pass the User specific property file name to the Conn.java class. So for that I am storing the property file name in cookie.

I have think one way of doing this:

  1. Get the cookie value in all servlets
  2. Pass the cookie value to the selectData() method of Select.java class And from that class
  3. pass the cookie value to the getConn() method of Conn.java class

So I want know if there is any better way to pass this Connection String file name to Conn.java class?

Thanks in advance.

2
  • The HttpSession handles the information related to the session. Typically you will store your user info there and let the server handle the details of how it stores it. Commented May 21, 2013 at 12:59
  • @SJuan76 yes HttpSession is the proper way, but I am using Cookies for sharing USERID across all tha web application deployed on same server Commented May 21, 2013 at 13:50

1 Answer 1

1

HttpSession is where user info should be stored (with some concerns).

In your case, where you seem to have many different web applications, each of them will have a different session, and you will need to update all of them.

I prefer another approach (and this is a personal opinion, which can be discussed) which is based in the ThreadLocal class. You can write a servlet filter, that will

  • read the cookie value
  • store it in a ThreadLocal
  • after the filter.doFilter method, you will have to clean it (This is extremely important, so you don't the have the chance of mixing sessions), just put the clean method in a finally block so it gets executed whatever happens.

The main advantage of this approach is that you may not have access to the HttpSession or HttpServletRequest, and you will still be able to get the value in the ThreadLocal.

An example of a ThreadLocal container you can use is this one :

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 you will be able to access the userId just by calling ThreadLocalContainer.getUserId() everywhere in your code, even if you don¡t have access to the http context.

Make sure you define the servlet filter in all your webapps, so the userId gets properly set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.