简体   繁体   中英

Can I create servlets at runtime?

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 eg 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 ie hold the menu and the page contents, which it renders based on data which has been set in an appropiate scope (eg request) by the Servlet.

JSP's are used for dynamic content. Use them

What specific feature are you implementing that would require you to create a new servlet per page? How were you thinking about doing this (dynamically generating Java code and compiling on the fly? byte code generation?)? Or are you thinking about mapping new URLs to an existing servlet? Either way, it doesn't really make a lot of sense to me.

It sounds to me like what you need it a CMS? There are some good discussions on Java-based CMSes here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM