2

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.

whirlwin
  • 16,044
  • 17
  • 67
  • 98

3 Answers3

4

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.

planetjones
  • 12,469
  • 5
  • 50
  • 51
1

JSP's are used for dynamic content. Use them

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

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

Community
  • 1
  • 1
Jack Leow
  • 21,945
  • 4
  • 50
  • 55
  • Unfortunately, Java based CMSes are often not always what you expect them to be when you have experience with PHP based CMSes like Drupal/Joomla/Wordpress. See also http://stackoverflow.com/questions/4410445/what-is-a-good-java-based-content-management-system-for-us-and-why – BalusC Jun 09 '11 at 14:01