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

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

问题描述

我需要弄清一些事情.我一直在寻找这个问题的答案,但是我似乎找不到对我的特定问题的好的答案(例如,这个问题正在on绕答案:据我了解,可以在面向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.

现在我的问题...

  • 何时使用一个或另一个?
  • 使用一种方法相对于另一种方法是否具有 general 的优势? (例如,通常推荐一种方法优于另一种方法吗?)
  • When do you use one or the other?
  • Are there any general advantages to use one method over the other? (Like, is one method recommended over the other in general?)

:重点关键字

推荐答案

如果您是对学习该语言感兴趣的学生,那么我现在将继续使用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的便捷方法,该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是最受欢迎的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天全站免登陆