简体   繁体   English

我可以在运行时创建 servlets 吗?

[英]Can I create servlets at runtime?

I have a website with several servlets.我有一个网站,里面有几个 servlets。 Each page like index, about, FAQ, contact us and so on, have a servlet connected to them.每个页面,如索引、关于、FAQ、联系我们等等,都有一个连接到它们的 servlet。 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.我还计划通过按钮create new page添加其他页面,这样当有人单击它时,将为该页面分配一个新的 servlet。

Is it possible to create servlets at runtime?是否可以在运行时创建 servlets? 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.如果这很重要,我正在使用 GlassFish。

No you can't create new servlets, but what you're suggesting is a common requirement.不,您不能创建新的 servlets,但您的建议是一个常见的要求。 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.然后对您的 servlet 进行编码,以根据数据库的内容动态呈现其页面菜单和每个单独的页面内容。

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).在此示例中,您的 servlet 可以扮演前端 Controller的角色,以处理确定已请求哪个视图并调度它的呈现的决策点(您可能需要一个单独的调度程序)。 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 在本质上可能非常通用,即保存菜单和页面内容,它根据 Servlet 在适当的 scope(例如请求)中设置的数据呈现这些内容。

JSP's are used for dynamic content. JSP 用于动态内容。 Use them使用它们

What specific feature are you implementing that would require you to create a new servlet per page?您正在实现哪些特定功能需要您为每页创建一个新的 servlet? How were you thinking about doing this (dynamically generating Java code and compiling on the fly? byte code generation?)?您是如何考虑这样做的(动态生成 Java 代码并即时编译?字节码生成?)? Or are you thinking about mapping new URLs to an existing servlet?或者您正在考虑将新的 URL 映射到现有的 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?这听起来像你需要它一个CMS? There are some good discussions on Java-based CMSes here这里有一些关于基于 Java 的 CMS 的很好的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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