-1

I am not sure if this question looks silly. But I am new to servlets. So posting my question here.

I have an application with around 5 servlets trhat needs to call application APIs after acquiring connection to application server(OIM). So I need to use existing connection from other servlets to OIM which was opened by frist servlet in the same war file.

My code is like this.

//Class 1
public class ValidateUserName extends HttpServlet {
//Servlet 1
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        ...
        ...
        UserOperations userOperations = new UserOperations();
        String CommonName = userOperations.getCommonName(userName);
        String userStatus =  userOperations.getUserStatus(userName);
        ...
        ...
        }
...
...
}

//Class 2
public class UserOperations {
public UserOperations() {
    String USERNAME = Utils.getProperty("USERNAME");
    String PASSWORD = Utils.getProperty("PASSWORD");
    String PROVIDER_URL = Utils.getProperty("PROVIDER_URL");

    OIMConnect oimConnect = new OIMConnect();
    this.oimClient = oimConnect.loginToOIM(USERNAME, PASSWORD, PROVIDER_URL);
}
...
...
}

//Class 3
public class PasswordReset extends HttpServlet {
//Servlet 2
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
         /*
          * I would like to use same oimConnect connection that was instantiated 
          * in UserOperations() constructor. 
          */
    }
    ...
    ...
    }

I can't use setAttribute as I don't have reference to OIM client on servlets. Connections to OIM server is getting opened in non servlet classes.

How can I use existing OIM connection across all servlets in this scenario?

user2961454
  • 349
  • 1
  • 3
  • 14

1 Answers1

0

You should define the connection factory as a JNDI resource so you can look it up from any of the servlets.

user207421
  • 289,834
  • 37
  • 266
  • 440