何时使用 Servlet 或 @Controller [英] When to use Servlet or @Controller

查看:17
本文介绍了何时使用 Servlet 或 @Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚一些事情.我一直在寻找这个问题的答案,但我似乎无法为我的具体问题找到一个好的答案(例如,这个问题正在蚕食答案:servlet 和网络服务的区别).

据我所知,在面向 MVC"的 Web 应用程序中,有多种不同的方式可以实现请求处理",也就是控制器",其中两种是:

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:

  1. 特定于 Java 的 Servlet(即您通过单击新建创建的一个 ->Servlet,例如在 eclipse 中),用作控制器".这个扩展 HttpServlet 并使用 doGetdoPost 等方法
  2. 一个带有 Spring MVC 注释的 @Controller 类(是的,使用了 DispatcherServlet).有了这个,你可以使用 @RequestMethod GET/POST
  1. 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 like doGet and doPost etc.
  2. A Spring MVC annotated @Controller class (yes, using a DispatcherServlet). With this one you use the @RequestMethod GET/POST etc.

现在我的问题...

  • 您什么时候使用其中一种?
  • 使用一种方法相对于另一种方法有任何一般优势吗? (例如,一种方法是否比另一种方法普遍推荐?)

:强调关键字

推荐答案

如果你是一名对学习这门语言感兴趣的学生,那么我现在会坚持使用 servlet.可以仅使用 servlet 编写 Web 应用程序,但在实践中,您可能也想查看 JSP.

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.

JSP 是编写 servlet 的一种便捷方式,它允许您将 html 与脚本元素混合(尽管建议避免在您的 jsp 中使用 Java 代码以支持标记和 el 表达式).在幕后,它将被编译为一个 servlet,但它避免了您必须使用大量杂乱的打印语句.

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.

至少对 servlet 和 JSP 有基本的了解很重要.Spring MVC 是构建在 servlet 之上的众多框架之一,它试图使编写 Web 应用程序的任务变得更容易一些.基本上所有请求都映射到 DispatcherServlet,它充当前端控制器.

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.

然后 DispatcherServlet 将调用其注解与传入请求匹配的控制器.这比必须自己在 web.xml 中编写这些映射更简洁(尽管在 servlet 3.0 中您现在可以注释 servlet).但是您还可以获得许多其他可以使用的好处,例如将表单字段映射到对象、使用 jsr303 注释验证该对象、将输入和输出映射到 xml 或 json 等.此外,它与核心 spring 紧密集成,因此您可以轻松连接供控制器调用的服务.

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.

值得注意的是,有大量构建在 servlet 之上的竞争框架.Spring MVC 是最流行的一种,因此研究它是一个不错的选择.

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 或 @Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆