每个路径一个Servlet或在一个Servlet中处理多个路径? [英] One Servlet per path or handle multiple paths in a Servlet?
问题描述
我想知道处理网站中的多个页面时处理servlet的最佳实践是什么. Servlet中的大多数GET请求都只是返回一个新的jsp页面.我想到了两种方法:
I'm wondering what is the best practice in handeling servlets when dealing with a many pages in a website. Most GET requests in servlets simply return a new jsp page. I had thought of two approaches:
-
为网站中的每个路径分配一个servlet,例如主页的homeservlet,注册页面的注册servlet,登录页面的登录servlet等.
Assign a servlet for each path in the webiste e.g a homeservlet for home page, registerservlet for registration page, loginservlet for login page etc.
具有少量servlet,它们可以处理多个路径并相应地处理它们,例如一个UserServlet,可以完成用户的注册和登录工作.但是我认为这种方法可能只会导致很多if语句,并且可能很难管理.理想的是在servlet中为每个路径分配一个方法(类似于Laravel的方法)
Have a small number of servlets that process multiple paths and handle them accordingly e.g. A UserServlet that could do the job of registration and logging in users. However I thought this approach might would just lead to lots of if statements and may be quite unmanageable. What would be ideal is assigning a method in a servlet to each path (similar to how Laravel does it)
推荐答案
通常情况下,最佳解决方案取决于上下文和必须提供的功能. 我认为在大多数情况下,允许从单个servlet处理多个路径将更为有效,因为它只允许针对多个情况开发一个servlet. 在以下情况下,最好将一组路径映射到单个servlet:
As it is often the case, what is best depends on the context and the functionality that must be offered. I would assume that in most cases allowing multiple paths to be handled from a single servlet would be more efficient as it allows the development of only one servlet for multiple cases. Mapping a group of paths to a single servlet would be preferable in the following cases:
- 这组路径提供的功能是相似的",可以由单个servlet处理,也许可以考虑实际路径.
- 应该提供的功能与用于调用servlet的实际路径无关.
不过,为每个servlet分配一个路径可能是有益的:
However it could be the cases that assigning a single path per servlet would be beneficial:
- servlet的功能应仅通过单个路径提供.
- 不同路径的功能不同,足以要求使用不同的Servlet类来实现它们.
这篇关于每个路径一个Servlet或在一个Servlet中处理多个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!