I have a website with several servlets. Each page like index, about, FAQ, contact us and so on, have a servlet connected to them. I am also planning to make it possible to add additional pages with a button create new page, so that when somebody clicks it, a new servlet will be assigned to that page.
Is it possible to create servlets at runtime? My guess is that it is not possible (or at least very difficult).
…If not are there any workarounds?
I am using GlassFish, if that matters.
No you can’t create new servlets, but what you’re suggesting is a common requirement. When a user creates a new page you need to save it somewhere e.g. database or file system. I would suggest the database.
Then code your servlet to dynamically render its menu of pages and each individual pages content based on the contents of your database.
In this example your servlet can play the role of a Front Side Controller to handle the decision points of determining which view has been requested and dispatching the rendering of it (you might want a separate Dispatcher). Your JSP could be very generic in nature i.e. hold the menu and the page contents, which it renders based on data which has been set in an appropiate scope (e.g. request) by the Servlet.