我读了很多文章来理解Java servlet,但我没有成功。
你能简单介绍一下Java servlet吗(用简单的语言)?什么是servlet?它的优点是什么?
我不能理解服务器端编程语言(PHP, ASP)和servlet之间的区别。
我读了很多文章来理解Java servlet,但我没有成功。
你能简单介绍一下Java servlet吗(用简单的语言)?什么是servlet?它的优点是什么?
我不能理解服务器端编程语言(PHP, ASP)和servlet之间的区别。
当前回答
您刚刚得到了一个正常servlet的答案。但是,我想与您分享一些关于Servlet 3.0的内容
What is first a Servlet? A servlet is a Web component that is managed by a container and generates dynamic content. Servlets are Java classes that are compiled to byte code that can be loaded dynamically into and run by a Java technology-enabled Web server or Servlet container. Servlet 3.0 is an update to the existing Servlet 2.5 specification. Servlet 3.0 required API of the Java Platform, Enterprise Edition 6. Servlet 3.0 is focussed on extensibility and web framework pluggability. Servlet 3.0 bring you up some extensions such as Ease of Development (EoD), Pluggability, Async Support and Security Enhancements Ease of Development You can declare Servlets, Filter, Listeners, Init Params, and almost everything can be configured by using annotations Pluggability You can create a sub-project or a module with a web-fragment.xml. It means that it allows to implement pluggable functional requirements independently. Async Support Servlet 3.0 provides the ability of asynchronous processing, for example: Waiting for a resource to become available, Generating response asynchronously. Security Enhancements Support for the authenticate, login and logout servlet security methods
我从Java Servlet教程中找到的
其他回答
Servlet是一个java类,用于响应HTTP请求并生成HTTP响应......当我们使用HTML创建页面时,它将是一个静态页面,因此为了使其动态,我们使用SERVLET{简单地说,人们可以理解}。 JSP克服了利用servlet的问题,它本身就使用了代码和HTML标记。
除了以上,只是要指出流血的明显……
To many this is hyper obvious, but to someone used to writing apps which are just run and then end: a servlet spends most of its time hanging around doing nothing... waiting to be sent something, a request, and then responding to it. For this reason a servlet has a lifetime: it is initalised and then waits around, responding to anything thrown at it, and is then destroyed. Which implies that it has to be created (and later destroyed) by something else (a framework), that it runs in its own thread or process, and that it does nothing unless asked to. And also that, by some means or other, a mechanism must be implemented whereby this "entity" can "listen" for requests.
我建议阅读有关线程、进程和套接字的内容可以让你对此有所了解:这与一个基本的“hello world”应用程序的功能方式是完全不同的。
有人可能会说,术语“服务器”或“servlet”有点大材小用。一个更合理、更简单的名字可能是“responder”。选择术语“服务器”的原因是历史的:第一个这样的安排是“文件服务器”,多个用户/客户端终端将从中央机器请求特定的文件,然后该文件将像一本书或一盘炸鱼和薯条一样被“提供”。
您刚刚得到了一个正常servlet的答案。但是,我想与您分享一些关于Servlet 3.0的内容
What is first a Servlet? A servlet is a Web component that is managed by a container and generates dynamic content. Servlets are Java classes that are compiled to byte code that can be loaded dynamically into and run by a Java technology-enabled Web server or Servlet container. Servlet 3.0 is an update to the existing Servlet 2.5 specification. Servlet 3.0 required API of the Java Platform, Enterprise Edition 6. Servlet 3.0 is focussed on extensibility and web framework pluggability. Servlet 3.0 bring you up some extensions such as Ease of Development (EoD), Pluggability, Async Support and Security Enhancements Ease of Development You can declare Servlets, Filter, Listeners, Init Params, and almost everything can be configured by using annotations Pluggability You can create a sub-project or a module with a web-fragment.xml. It means that it allows to implement pluggable functional requirements independently. Async Support Servlet 3.0 provides the ability of asynchronous processing, for example: Waiting for a resource to become available, Generating response asynchronously. Security Enhancements Support for the authenticate, login and logout servlet security methods
我从Java Servlet教程中找到的
如果你是初学者,我认为本教程可能会给出什么是Servlet的基本概念…
以下是一些有价值的观点。
利用Servlet技术创建web应用程序,该应用程序驻留在服务器端,生成动态网页。
Servlet可以根据上下文以多种方式描述。
Servlet是一种用于创建web应用程序的技术。 Servlet是一个API,它提供了许多接口和类,包括 文件。 Servlet是一个必须实现的接口,用于创建任何 servlet。 Servlet是一个扩展服务器和服务器功能的类 响应传入的请求。它可以对任何类型的 请求。 Servlet是部署在服务器上用于创建的web组件 动态网页。 参考:在这里。
A servlet at its very core is a java class; which can handle HTTP requests. Typically the internal nitty-gritty of reading a HTTP request and response over the wire is taken care of by the containers like Tomcat. This is done so that as a server side developer you can focus on what to do with the HTTP request and responses and not bother about dealing with code that deals with networking etc. The container will take care of things like wrapping the whole thing in a HTTP response object and send it over to the client (say a browser).
Now the next logical question to ask is who decides what is a container supposed to do? And the answer is; In Java world at least It is guided (note I did not use the word controlled) by specifications. For example Servlet specifications (See resource 2) dictates what a servlet must be able to do. So if you can write an implementation for the specification, congratulations you just created a container (Technically containers like Tomcat also implement other specifications and do tricky stuff like custom class loaders etc but you get the idea).
假设您有一个容器,您的servlet现在是java类,其生命周期将由容器维护,但它们对传入HTTP请求的反应将由您决定。你可以通过在init()、doGet()、doPost()等预定义方法中编写你想要做的事情来做到这一点。看看资源3。
这里有一个有趣的练习。像资源3中那样创建一个简单的servlet,在它的构造函数方法(是的,你可以有一个servlet的构造函数)、init()、doGet()、doPost()方法中编写一些System.out.println()语句,并在tomcat中运行servlet。查看控制台日志和tomcat日志。
资源
看看这里的HTTP servlet (Tomcat示例)。 Servlet规范。 简单的Servlet示例。 开始在线阅读这本书/PDF 它还提供了整本书的下载。也许这会有帮助。 如果你刚刚开始使用servlet,那么阅读相关材料和servlet API是一个不错的主意。这是一个较慢的学习过程,但对弄清基本知识更有帮助。