Link to home
Start Free TrialLog in
Avatar of tcollogne
tcollogne

asked on

Create a webapp around an existing webapp for handling session control


Hi all,

A time ago I posted this question https://www.experts-exchange.com/questions/21650462/Create-a-webapp-around-an-existing-webapp-for-handling-session-control.html#15405554

The result of that question was to use tomcat valves. So far so good. In a couple of weeks, I will begin development of the application described in the above question.
I have one reservation left.

Are these Valves thread-safe? Does anyone know about this?

Thank you.
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

I would treat them like servlets, so any static or global members should be handles with care, but otherwise all method level variables should be fine :-)

Tim
Avatar of tcollogne
tcollogne

ASKER

So, I can use private methods inside the class with no problem like this. The getDatabaseUser is a private method that I created. It will get information from inside a database (JDBC).

public class TomcatValveEx extends ValveBase {
 
   public void invoke(Request request, Response response, ValveContext context) throws IOException, ServletException {

             System.out.println("Example VALVE hit");
             context.invokeNext(request, response);
   }

   private String getDatabaseUser() {
      return "test";
   }

}
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am going to keep this open for a little longer.
Indeed, I can't seem to manage getting a DataSource in the valve.
I get a javax.naming.NameNotFoundException : Name comp is not bound in this Context.

I use this code for getting a datasource :

Context initContext = new InitialContext();                        
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)initContext.lookup("jdbc/vtvres");            
Connection conn = ds.getConnection();

Is there another way of achieving it?
Sorry the code is

Context initContext = new InitialContext();                  
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/vtvres");            
Connection conn = ds.getConnection();