每个webapp一个或多个servlet?

时间:2020-12-29 21:02:00

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实现此功能,还是将其分配给函数。至少有很多东西是共享的,比如http-headers。

So, what are your experiences, what works best?

那么,你的经历是什么,最有效的是什么?

3 个解决方案

#1


10  

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.

简而言之,您将为每个webapp创建许多servlet,因为每个webapp都会暴露几个用例。

[EDIT] Re-reading your question it seems as if you are using the term site to 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。

#2


8  

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可以使代码流集中。

#3


3  

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的对象,这些对象以依赖注入方式实例化。例如,我的s​​ervlet中有类似的东西(伪代码):

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(除非你使用像我的d-i方法)。

#1


10  

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.

简而言之,您将为每个webapp创建许多servlet,因为每个webapp都会暴露几个用例。

[EDIT] Re-reading your question it seems as if you are using the term site to 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。

#2


8  

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可以使代码流集中。

#3


3  

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的对象,这些对象以依赖注入方式实例化。例如,我的s​​ervlet中有类似的东西(伪代码):

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(除非你使用像我的d-i方法)。