Java 何时使用 Servlet 或 @Controller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16439249/
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
When to use Servlet or @Controller
提问by Roger
I need to get a few things cleared up. I have been looking for an answer for this one, but I can't seem to find a good answer to my specific questions (eg. this question was nibbling on the answer: Difference between servlet and web service).
我需要弄清楚一些事情。我一直在寻找这个问题的答案,但我似乎无法为我的具体问题找到一个好的答案(例如,这个问题正在蚕食答案:Difference between servlet and web service)。
To my understanding, there are different ways you can implement the "request handling", aka "Controller", in an "MVC oriented" web application, two of them being:
据我了解,在“面向 MVC”的 Web 应用程序中,有多种不同的方式可以实现“请求处理”,也就是“控制器”,其中两种是:
- A Java specific Servlet(ie. one you create by clicking new ->
Servlet, in eclipse for example), used as a "Controller". This one
extends
HttpServlet
and you use methods likedoGet
anddoPost
etc. - A Spring MVC annotated
@Controller
class (yes, using aDispatcherServlet
). With this one you use the@RequestMethod
GET
/POST
etc.
- 一个 Java 特定的Servlet(例如,您通过单击 new -> Servlet 创建的一个,例如在 eclipse 中),用作“控制器”。这一个延伸
HttpServlet
,并且使用类似的方法doGet
和doPost
等。 - 一个 Spring MVC 注释
@Controller
类(是的,使用DispatcherServlet
)。有了这个,您可以使用@RequestMethod
GET
/POST
等。
Now to my questions...
现在我的问题...
- When do you use one or the other?
- Are there any generaladvantages to use one method over the other?(Like, is one method recommended over the other in general?)
- 你什么时候使用其中一个?
- 使用一种方法比另一种方法有什么普遍的优势吗?(比如,通常推荐一种方法而不是另一种方法吗?)
[EDIT]: Emphasized keywords
[编辑]:强调关键字
采纳答案by Ben Thurley
If you're a student interested in learning the language then I would stick with servlets for now. It's possible to write a web app using just servlets but in practice you'll probably want to look at JSP's too.
如果你是一个对学习这门语言感兴趣的学生,那么我现在会坚持使用 servlet。可以仅使用 servlet 编写 Web 应用程序,但在实践中,您可能也想查看 JSP。
A JSP is a convenient way to write a servlet that allows you to mix html with scripting elements (although it's recommended to avoid Java code in your jsp in favour of tags and el expressions). Under the covers it will be compiled as a servlet but it avoids you having to use lots of messy print statements.
JSP 是编写 servlet 的一种便捷方式,它允许您将 html 与脚本元素混合(尽管建议避免在您的 jsp 中使用 Java 代码以支持标记和 el 表达式)。在幕后,它将被编译为 servlet,但它避免了您必须使用大量杂乱的打印语句。
It's important to have at least a basic understanding of servlets and JSP's. Spring MVC is one of many frameworks built on top of servlets to try make the task of writing a web application a bit easier. Basically all requests are mapped to the DispatcherServlet which acts as a front controller.
至少对 servlet 和 JSP 有基本的了解是很重要的。Spring MVC 是构建在 servlet 之上的众多框架之一,它试图使编写 Web 应用程序的任务变得更容易一些。基本上所有的请求都映射到作为前端控制器的 DispatcherServlet 。
The DispatcherServlet will then call the controller whose annotations match the incoming request. This is neater than having to write these mappings yourself in the web.xml (although with servlet 3.0 you can annotate servlets now). But you also get many other benefits that can be used like mapping form fields to an object, validating that object with jsr303 annotations, map inputs and outputs to xml or json etc etc. Plus it's tightly integrated with core spring so you can easily wire in your services for the controller to call.
然后 DispatcherServlet 将调用其注解与传入请求匹配的控制器。这比必须自己在 web.xml 中编写这些映射更简洁(尽管在 servlet 3.0 中您现在可以注释 servlet)。但是您还可以获得许多其他可以使用的好处,例如将表单字段映射到对象、使用 jsr303 注释验证该对象、将输入和输出映射到 xml 或 json 等。此外,它与核心 spring 紧密集成,因此您可以轻松连接供控制器调用的服务。
It's worth noting that there are a plethora of competing frameworks built on top of servlets. Spring MVC is one of the most popular so it's not a bad choice to look into.
值得注意的是,有大量构建在 servlet 之上的竞争框架。Spring MVC 是最流行的一种,所以研究它并不是一个糟糕的选择。
回答by Lukas Eichler
JSF and JSP aswell as Spring MVC builts upon Servlets. The problem this that servlets are not very "nice" to work with because you have to write direct html.
JSF 和 JSP 以及 Spring MVC 都建立在 Servlet 之上。servlet 不是很“好”处理这个问题,因为您必须直接编写 html。
If you are able to use mordern web technologies I would just use servlets in positions that need direct http output like writing an image from a database to http.
如果您能够使用现代 Web 技术,我只会在需要直接 http 输出的位置使用 servlet,例如将图像从数据库写入 http。
Using SpringMVC or JSF which work with a Dipatcherservlet or FacesServlet is just faster and more fun. They parse your files and send it through a servlet.
使用与 Dipatcherservlet 或 FacesServlet 一起工作的 SpringMVC 或 JSF 会更快更有趣。他们解析您的文件并通过 servlet 发送它。
回答by Giovanni
A Servlet and a Spring MVC Controller can be used to do the same thing but they act on different level of a Java Application
Servlet 和 Spring MVC 控制器可用于做同样的事情,但它们作用于 Java 应用程序的不同级别
The servlet is a part of the J2EE framework and every Java application server (Tomcat, Jetty, etc) is built for running servlets. Servlet are the "low level" layer in the J2EE stack. You don't need a servlet.jar to run you application because it's prepackaged with the application server
servlet 是 J2EE 框架的一部分,每个 Java 应用服务器(Tomcat、Jetty 等)都是为运行 servlet 而构建的。Servlet 是 J2EE 堆栈中的“低级”层。您不需要 servlet.jar 来运行您的应用程序,因为它与应用程序服务器一起预先打包
A Spring MVC controller is a library built upon the servlet to make things easier. Spring MVC offers more built-in functionalities such as form parameter to controller method parameter mapping, easier handling of binary form submissions (i.e. when your form can upload files). You need to package the required jars to your application in order to run a Spring MVC Controller
Spring MVC 控制器是一个构建在 servlet 上的库,使事情变得更容易。Spring MVC 提供了更多的内置功能,例如表单参数到控制器方法参数的映射,更容易处理二进制表单提交(即当您的表单可以上传文件时)。您需要将所需的 jars 打包到您的应用程序中才能运行 Spring MVC 控制器
You should use a servlet when you need to go "low level", and example could be for performance reason. Spring MVC performs good but if it has some overhead, if you need to squeeze out all you can from your application server (and you have already tuned the other layers such as the db) go with a servlet. You can choose a servlet if you want to understand the foundation of the J2EE web specifications (i.e. for educational purposes)
当您需要进入“低级别”时,您应该使用 servlet,示例可能是出于性能原因。Spring MVC 表现良好,但如果它有一些开销,如果您需要从应用程序服务器中挤出所有可用的东西(并且您已经调整了其他层,例如 db),请使用 servlet。如果您想了解 J2EE Web 规范的基础(即用于教育目的),您可以选择 servlet
In all the other cases you can/should choose a web framework. Spring MVC is one of them; with Spring MVC you don't need to reinvent the wheel (i.e binary form management, form parameter to bean conversion, parameter validation and so on). Another plus of Spring MVC is that in one class you can easily manage input from different urls and methods, doing the same in a servlet is possible but the code is more complicated and less readable. My opinion is that Spring MVC is good for building rest services and managing simple applications (i.e web application with simple forms). If you need to manager very complex forms with Ajax, nested forms, and an application with both session and page state my advice is to switch to a component based framework (like apache wicketfor example).
在所有其他情况下,您可以/应该选择一个 Web 框架。Spring MVC 就是其中之一;使用 Spring MVC,您无需重新发明轮子(即二进制表单管理、表单参数到 bean 的转换、参数验证等)。Spring MVC 的另一个优点是,在一个类中,您可以轻松管理来自不同 url 和方法的输入,在 servlet 中执行相同操作是可能的,但代码更复杂且可读性更差。我的观点是 Spring MVC 非常适合构建休息服务和管理简单的应用程序(即具有简单表单的 Web 应用程序)。如果您需要使用 Ajax 管理非常复杂的表单、嵌套表单以及具有会话和页面状态的应用程序,我的建议是切换到基于组件的框架(例如apache wicket)。