java servlet 中“主要方法”的作用是什么?

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

What acts in the role of the 'main method' in a servlet?

javaservlets

提问by giri

Servlet is also java program but there is no main method in servlet.Who will take role of main method on servet.

servlet也是java程序,但是servlet中没有main方法,谁来担当servert上的main方法。

回答by Asaph

Servlets are designed to run inside a servlet container (eg. Apache Tomcat). Execution of a servlet happens in the following manner: The servlet container calls the GenericServlet.service()method on a servlet which typically calls the appropriate doXxx()method, eg. doGet(), doPost(), etc. The doXxx()method is responsible for interpreting the HTTP request and serving an appropriate response. GenericServlet.service()is roughly analagous to main()in a plain old java class.

Servlet 被设计为在 servlet 容器(例如 Apache Tomcat)内运行。servlet 的执行以下列方式发生:servlet 容器调用 servlet 上的GenericServlet.service()方法,该方法通常调用适当的doXxx()方法,例如。doGet()doPost()等。该doXxx()方法负责解释 HTTP 请求并提供适当的响应。GenericServlet.service()大致类似于main()普通的旧 Java 类。

回答by Varun

Servlet runs inside a container(eg:tomcat). This container perform its work under jvm. Here container takes "the absence of main method". In simlple java program main method tells the starting control flow of the execution. But in case of servlet base web application jvm dose not need to search the main method. Servlet container tells jvm about the starting control flow.

Servlet 在容器内运行(例如:tomcat)。这个容器在 jvm 下执行它的工作。这里容器采用“缺少 main 方法”。在简单的java程序中,main方法告诉执行的启动控制流程。但是对于基于 servlet 的 Web 应用程序,jvm 不需要搜索 main 方法。Servlet 容器告诉 jvm 有关启动控制流的信息。

回答by bryantsai

Servlet are deployed on Java application server (servlet container). They are kind of 'passive'. When you write servlet, your servlet code is called by the container whenever there's request or need. So you don't see 'main' in your servlet (the whole thing is not started from servlet), which is inside application server (you could imagine the startup of application server starts from some kind of main).

Servlet 部署在 Java 应用服务器(servlet 容器)上。他们有点“被动”。当您编写 servlet 时,只要有请求或需要,容器就会调用您的 servlet 代码。所以你在你的servlet中看不到'main'(整个事情不是从servlet启动的),它在应用服务器内部(你可以想象应用服务器的启动是从某种main启动的)。

回答by jbruce2112

If you're looking for an area in a servlet to place code that's run on startup (similar to main()), take a look at implementing the ServletContextListenerinterface.

如果您正在 servlet 中寻找一个区域来放置在启动时运行的代码(类似于 main()),请查看实现ServletContextListener接口。

Its two methods are called on application startup and shutdown.

它的两个方法在应用程序启动和关闭时调用。

回答by mattjames

There is no main method in a Java servlet any more than than an ActionListener on a Swing JButton has a main method. What they both do have are methods that you can hook into when a certain event happens (a click on the JButton for example, or an HTTP PUT request on a HttpServlet). And in both cases, you are provided information about the event that triggered the call - the ActionEvent for the JButton and the ServletRequest for a servlet.

Java servlet 中没有 main 方法,就像 Swing JButton 上的 ActionListener 有一个 main 方法一样。它们都有一些方法,您可以在发生特定事件时挂钩(例如,单击 JButton,或对 HttpServlet 发出 HTTP PUT 请求)。在这两种情况下,您都会获得有关触发调用的事件的信息 - JButton 的 ActionEvent 和 servlet 的 ServletRequest。

Thinking of servlets in terms of event handlers is probably more useful than trying to think of them like a standalone Java application where you are responsible for the entire control flow.

从事件处理程序的角度考虑 servlet 可能比试图将它们视为一个独立的 Java 应用程序更有用,在该应用程序中您负责整个控制流。