Java 每个 webapp 一个或多个 servlet?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/272118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:20:47  来源:igfitidea点击:

One or multiple servlets per webapp?

javaservletsweb-applications

提问by Mnementh

I know, it depends on the webapp. But in the normal case, what do you do: one servlet, that serves different pages (like an standalone-application with changing content) or for every page a single servlet.

我知道,这取决于 webapp。但在正常情况下,您会做什么:一个 servlet,它为不同的页面提供服务(如具有不断变化的内容的独立应用程序),或者为每个页面提供一个 servlet。

Take for instance a blog. There is the start-page with the most recent blog-entries, an article-view for displaying one blog-entry and an archive. Do you implement this with three different servlets, or one that is dispatching to the functions. At least a good part of the stuff is shared, like http-headers.

以博客为例。有一个包含最新博客条目的起始页、一个用于显示一个博客条目的文章视图和一个存档。您是使用三个不同的 servlet 来实现这一点的,还是使用一个分派给函数的 servlet 来实现的。至少有很大一部分内容是共享的,例如 http-headers。

So, what are your experiences, what works best?

那么,你有什么经验,什么最有效?

回答by Vincent Ramdhanie

Usually you will create a servlet per use case. Servlets acts like controllers for your application. When you identify an interaction from a user then implement a servlet to control that interaction.

通常您会为每个用例创建一个 servlet。Servlet 就像应用程序的控制器一样。当您识别来自用户的交互时,然后实现一个 servlet 来控制该交互。

That is, if you are using plain servlet/JSP to build the site. If you are using a framework like struts you will find that they implement the front controller pattern and use a single servlet that recieves all the requests and forwards these requests to action classes that implement the actual logic of the user request. this is much harder to do yourself but its a good practice...its the reason why so many people use these frameworks.

也就是说,如果您使用普通的 servlet/JSP 来构建站点。如果您正在使用像 struts 这样的框架,您会发现它们实现了前端控制器模式并使用单个 servlet 接收所有请求并将这些请求转发到实现用户请求实际逻辑的操作类。这很难自己做,但这是一个很好的做法……这就是为什么这么多人使用这些框架的原因。

So the short answer is, you will create many servlets per webapp since each webapp will expose several use cases.

所以简短的回答是,您将为每个 Web 应用程序创建许多 servlet,因为每个 Web 应用程序将公开几个用例。

[EDIT] Re-reading your question it seems as if you are using the term siteto mean page or view. Again, it depends on what is happening on that view. For instance, To display the newest blog entry, you can have a servlet that constructs the list of entries from the database for display. If the user clicks on an entry then another servlet can retrieve that single entry for viewing and so on. Mainly, each action is a use case therefore a different servlet.

[编辑] 重新阅读您的问题,似乎您使用术语站点来表示页面或查看。同样,这取决于该视图中发生的情况。例如,要显示最新的博客条目,您可以使用一个 servlet 来构建数据库中的条目列表以供显示。如果用户单击一个条目,则另一个 servlet 可以检索该单个条目以供查看等等。主要是,每个动作都是一个用例,因此是一个不同的 servlet。

回答by Miguel Ping

Most web frameworks use a dispatcher servlet (ex: Spring MVC) that takes care of routing requests to appropriate classes/controllers.

大多数 Web 框架使用调度程序 servlet(例如:Spring MVC)负责将请求路由到适当的类/控制器。

When you start having lots of pages, this approach works best because you have a more user friendly way (in regard to web.xml) of declaring/managing a class that handles http requests and its url. Example (spring mvc again):

当您开始拥有大量页面时,这种方法最有效,因为您有一种更用户友好的方式(关于 web.xml)来声明/管理处理 http 请求及其 url 的类。示例(再次弹簧 mvc):

@Controller
public class MyController {
 @RequestMapping("/viewPosts")
 public void doViewPosts(HttpRequest r, HttpResponse res) {
  //...
 }
}

Besides, having a dispatcher servlet keeps your code flow centralized.

此外,拥有调度程序 servlet 可以使您的代码流集中。

回答by alex

It depends.

这取决于。

In my latest projects, I have implemented a single servlet that delegates to several servlet-like objects which are instantiated in a dependency injection fashion. For instance, I have something like this in my servlet (pseudo-code):

在我最近的项目中,我实现了一个 servlet,该 servlet 委托给几个以依赖注入方式实例化的类 servlet 对象。例如,我的 servlet 中有这样的东西(伪代码):

for(Handler handler : handlers) {
    if(handler.handle(request, response)) {
         return;
    }
}

where Handler is an interface with a boolean handle(request, response) method. I obtain my handlers from a container (be it Spring or something even more lightweight).

其中 Handler 是一个带有布尔句柄(请求,响应)方法的接口。我从容器(无论是 Spring 还是更轻量级的东西)获取我的处理程序。

The reason for this is that I really like dependency injection, and it is difficult to achieve it in Servlets; and I really don't feel much at home with most frameworks that provide web-component dependency injection- I like the simplicity of servlets.

这样做的原因是我真的很喜欢依赖注入,在Servlets中很难实现;并且我对大多数提供 Web 组件依赖注入的框架确实不太熟悉——我喜欢 servlet 的简单性。

Were not for this, I would go with multiple servlets, although there's a trade-off; either you have an enormous web xml with lots (and lots) of servlet mappings or you have a very complex servlet (unless you use something like my d-i approach).

如果不是为了这个,我会使用多个 servlet,尽管需要权衡;要么你有一个巨大的 web xml,其中有很多(很多)servlet 映射,要么你有一个非常复杂的 servlet(除非你使用类似我的 di 方法)。