0

I have a Java Web Application that currently uses a JDBC resource and connection pool to connect to a database. Currently, I only connect to one database at a time. I would like to be able to run the same code but connect to different databases by including something in the URL. I need to be able to still access my servlets by URL too. For example, the following links would run the same Servlet code, but for different databases. www.myapp.com/database1/Scheduling www.myapp.com/database2/Scheduling

I would like the database configurations to be outside of the web application, so that when I add a new connection or edit a connection, I don't have to rebuild my war file. I think I can do this by keeping the configuration file on the web server and editing that instead. I picture having the end user launch the app and choose the database they want to connect to. Once they choose, I could then grab the connection details.

There is also the possibility that a user could have 2 databases/links open at once, and I need to manage that/make it possible. I assume that would deal more with the Sessions.

I think I'm getting hung up on how I would be able to parse the database1 piece out of the URL and still be able to access the servlet. I understand how to map servlets in the web.xml file, but I don't know that what I'm describing is even possible.

I've been searching for a similar problem or question, but it seems the idea of having a web application that connects to different databases is more about accessing different components, such as a customer database and a product database.

Is what I am describing possible? Can this be accomplished in another way than I am describing?

I'm looking at Hibernate and also considered the Spring Framework. I think I'm a bit paralyzed at this point, not wanting to go down a path for something that isn't feasible or possible.

Thank you very much in advance

1 Answers1

0

Is what I am describing possible?

Yes. Use a Servlet Filter.

Here's a kickoff example, the implementation should speak for itself:

@WebFilter("/*")
public class DatabasePathFilter extends HttpFilter {

    private static final String DATABASE_PATH = "/database";

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String ctx = req.getContextPath();
        String uri = req.getRequestURI();

        if (uri.startsWith(ctx + DATABASE_PATH)) {
            String[] paths = uri.substring(ctx.length() + 1).split("/", 2);
            String databasePath = paths[0];
            req.setAttribute("databasePath", databasePath);
            String servletPath = paths[1];
            String servletUri = ctx + "/" + servletPath;
            req.getRequestDispatcher(servletUri).forward(req, res);
        }
        else {
            chain.doFilter(req, res);
        }
    }
}

In any servlet you can if necessary retrieve the database path by req.getAttribute("databasePath").

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much BalusC. Sounds like a clear cut solution. This will also keep the value databasePath in the URL I assume? I'll give it a try. – Jessica Love Jul 07 '23 at 13:12
  • Of course. The code is performing a forward not a redirect. See also https://stackoverflow.com/a/2048640 – BalusC Jul 07 '23 at 13:14
  • Ah, duh. My bad for such silly questions. Thank you very much. Very thankful to receive answers from a member such as yourself. – Jessica Love Jul 07 '23 at 15:40