Java 为什么 HTTPServlet 是一个抽象类?任何功能原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18909206/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:13:54  来源:igfitidea点击:

Why HTTPServlet is an abstract class? Any functional reason?

javaservletsabstract-classprivate-constructor

提问by rockyPeoplesChamp

HttpServletis an abstract class with all implemented methods. Why it is abstract?

HttpServlet是一个包含所有已实现方法的抽象类。为什么是抽象的?

The most common answer I got is, to restrict the instantiation of HttpServlet. But there are other ways of doing it, like a private constructor will restrict the instantiation.

我得到的最常见的答案是,限制HttpServlet. 但是还有其他方法可以做到,例如私有构造函数会限制实例化。

I can understand that they are following Template Method design pattern. If some methods are abstract, user will end up implementing all of them, even if he does not need them for his business logic.

我可以理解他们遵循模板方法设计模式。如果某些方法是抽象的,用户最终将实现所有这些方法,即使他的业务逻辑不需要它们。

But if HttpServletwas not abstract, an user can still extend it and override the require methods.

但是如果HttpServlet不是抽象的,用户仍然可以扩展它并覆盖 require 方法。

At least by the dictionary meaning of the word 'abstract', its does not make any sense to me to have a abstract class with all implemented method.

至少根据“抽象”这个词的字典含义,拥有一个包含所有实现方法的抽象类对我来说没有任何意义。

Yes a combination of abstract and concrete methods are ok to have.

是的,抽象和具体方法的组合是可以的。

But if you are making a class abstract why not make those methods abstract which the sub class has to override? or may be do not declare it as abstract at all?

但是,如果您要使类抽象,为什么不使子类必须覆盖的那些方法是抽象的呢?或者可能根本不将其声明为抽象?

Like doGet()or doPost()is this case.

喜欢doGet()或者doPost()是这种情况。

采纳答案by Thomas W

To have any useful behaviour, it is expected that you will have to override the methods. HttpServlet does not have useful functionality on its own.

要获得任何有用的行为,预计您必须覆盖这些方法。HttpServlet 本身没有有用的功能。

Making its constructors private would limit the ability for subclasses to be created.

将其构造函数设为私有将限制创建子类的能力。

The design of HttpServlet was probably not ideal -- as on many pages, forms especially, GET and POST logic should proceed at least partly along a common path. The design idea of HttpServlet however was to offer doGet(), doPost()etc implementations answering a 'not supported' error depending on HTTP version. These stubs would be useful to inherit if you needed to return such an answer.

HttpServlet 的设计可能并不理想——因为在许多页面上,尤其是表单上,GET 和 POST 逻辑应该至少部分地沿着一条公共路径进行。然而HttpServlet的的设计理念是报价doGet()doPost()等实现接听根据HTTP版本“不支持”的错误。如果您需要返回这样的答案,继承这些存根将很有用。

In summary, the API/ interfaceis complete -- but the functionalityis definitively not. Thus it is declared as abstract.

总之,API/接口是完整的——但功能绝对不是。因此它被声明为抽象的

回答by Stephen C

HTTPServlet is an abstract class with all implemented methods. Then why it is abstract ?

HTTPServlet 是一个包含所有已实现方法的抽象类。那为什么是抽象的呢?

It is abstract because the implementations of key methods have to be provided by (e.g. overridden by) a custom servlet class. As the javadoc says:

它是抽象的,因为关键方法的实现必须由自定义 servlet 类提供(例如覆盖)。正如 javadoc 所说:

A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet
  • getServletInfo, which the servlet uses to provide information about itself

HttpServlet 的子类必须至少覆盖一种方法,通常是以下方法之一:

  • doGet,如果 servlet 支持 HTTP GET 请求
  • doPost,用于 HTTP POST 请求
  • doPut,用于 HTTP PUT 请求
  • doDelete,用于 HTTP DELETE 请求
  • init 和 destroy,用于管理在 servlet 生命周期内保留的资源
  • getServletInfo,servlet 使用它来提供有关自身的信息

If you extend the class without overriding any methods, you will get a useless servlet; i.e. one that gives an error response for all requests. Similarly, if the class was not abstract, then any direct instance of HttpServletwould be useless.

如果你扩展类而不覆盖任何方法,你将得到一个无用的 servlet;即为所有请求提供错误响应的一种。类似地,如果类不是abstract,那么任何直接的 实例HttpServlet都将无用。

Hence, the reason for making the HttpServletclass abstractis to prevent a (naive) programmer error.

因此,创建HttpServlet类的原因abstract是为了防止(天真的)程序员错误。



For the record, the reason for providing implementations of all of the methods is to make life simpler for the programmer by providing default behaviour. For instance, if I don't want my servlet to support DELETE requests, the default implementation for doDeletewill conveniently send a response with the "Method not supported" response code.

作为记录,提供所有方法的实现的原因是通过提供默认行为使程序员的生活更简单。例如,如果我不希望我的 servlet 支持 DELETE 请求,默认实现doDelete将方便地发送带有“方法不支持”响应代码的响应。

回答by brajesh

You are forced to extend HttpServlet because you need to add your application specific logic to it. Here is the definition of an abstract class according to oracle's men:

您被迫扩展 HttpServlet,因为您需要向其添加应用程序特定的逻辑。这是根据oracle's men的抽象类的定义:

"An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed."

“抽象类是一个声明为抽象的类——它可能包含也可能不包含抽象方法。抽象类不能被实例化,但它们可以被子类化。”

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Reason: We all know that HttpServlet doesnot have any abstract method. It contains all concrete methods.But still this class is kept abstract.The reason is very simple. Our own class can act as a Servlet,only when it extends HttpServlet or GenericServlet class,or implements Servlet interface If HttpServlet class will not be kept abstract,you will not be intrested to extend this class,and your class will not act as Servlet.

原因:我们都知道HttpServlet没有任何抽象方法。它包含了所有具体的方法。但是这个类仍然保持抽象。原因很简单。我们自己的类可以作为一个 Servlet,只有当它扩展 HttpServlet 或 GenericServlet 类,或者实现了 Servlet 接口时,如果 HttpServlet 类不保持抽象,你就没有兴趣扩展这个类,你的类也不会充当 Servlet。

ServletContainer uses instanceOf() to know if your class is the child of HttpServlet or GenericServlet or Servlet interface. Since your class is not the child of HttpServlet,GenericServlet class or implemented Servlet interface,instanceOf() will fail.

ServletContainer 使用 instanceOf() 来知道您的类是否是 HttpServlet 或 GenericServlet 或 Servlet 接口的子类。由于您的类不是 HttpServlet、GenericServlet 类或实现的 Servlet 接口的子类,instanceOf() 将失败。

回答by Sarvesh Kaushik

Basically we have here is an abstract class (HttpServlet) without any abstract method or only concrete method. Where our servlet class implements javax.servlet.Servletdirectly (in case of RMI and CORBA) or indirectly (extending generic or HTTPServlet).

基本上我们这里是一个HttpServlet没有任何抽象方法或只有具体方法的抽象类()。我们的 servlet 类javax.servlet.Servlet直接(在 RMI 和 CORBA 的情况下)或间接(扩展泛型或HTTPServlet)实现的地方。

As Interface has 3 main methods (init(), service()and destroy()) which is implemented by the HttpServlet(abstract class) which is extended by your servlet class which process the browser requests made to the server using these three methods. And depending on type of HTTP request method, our servlet class (by extending HttpServlet) uses the respective do[xxx]method which is in majority of cases is doGetor doPost. If we have all the methods or some of methods of httpServlet as abstract method, we have to implement all or some of the abstract method which are present in HttpServletin our servlet subclass but we have to implement only those methods which are required to process the HTTP method request. Thus according to me having concrete method in abstract class provides freedom of implementation depending upon the logic of HTTP request.

由于 Interface 有 3 个主要方法(init(),service()destroy()),它们由HttpServlet(抽象类)实现,该抽象类由您的 servlet 类扩展,该类使用这三种方法处理向服务器发出的浏览器请求。并且根据 HTTP 请求方法的类型,我们的 servlet 类(通过扩展HttpServlet)使用各自的do[xxx]方法,在大多数情况下是doGetdoPost。如果我们有 httpServlet 的所有方法或部分方法作为抽象方法,我们必须实现所有或部分存在于HttpServlet在我们的 servlet 子类中,但我们只需要实现处理 HTTP 方法请求所需的那些方法。因此,根据我的说法,抽象类中的具体方法根据 HTTP 请求的逻辑提供了实现的自由。

回答by Devender Kumar

There are two reason for this.

这有两个原因。

  1. First So that you can not create Object of HttpServlet Class. Let suppose it were not protected then we could create obj of this class which would we useless.
  2. Second There are protected method in HttpServlet class. So to access protected thing of a class in order to have Servlet feature in your implementation class you must extend that class. So basically it is to force programmer to extend HttpServlet Class.
  1. 首先让你不能创建 HttpServlet 类的对象。假设它不受保护,那么我们可以创建这个类的 obj ,这将是我们无用的。
  2. 其次 HttpServlet 类中有 protected 方法。因此,要访问类的受保护事物以便在您的实现类中具有 Servlet 功能,您必须扩展该类。所以基本上就是强制程序员扩展HttpServlet Class。

回答by Venu Palabandla

Basically HttpServlet does not contains any abstract method, its only implementing life cycle service (--,--) method which is abstract. And also it is providing 7 doXXX() non abstract methods without having any application related logic to send 404 error as response. So extended class of HTTPServlet class is no need to implement doXXX() methods to inform Java developers that HttpServlet class is not fully implemented class HttpServlet class made as abstract.

基本上HttpServlet 不包含任何抽象方法,它唯一实现的生命周期服务(--,--) 方法是抽象的。并且它还提供了 7 个 doXXX() 非抽象方法,而没有任何与应用程序相关的逻辑来发送 404 错误作为响应。因此,HTTPServlet 类的扩展类不需要实现 doXXX() 方法来通知 Java 开发人员 HttpServlet 类不是完全实现的类 HttpServlet 类作为抽象。