我读了很多文章来理解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?

A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request. Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide. Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat. In a request/response paradigm, a web server can serve only static pages to the client To serve dynamic pages, a we require Servlets. Servlet is nothing but a Java program This Java program doesn’t have a main method. It only has some callback methods. How does the web server communicate to the servlet? Via container or Servlet engine. Servlet lives and dies within a web container. Web container is responsible for invoking methods in a servlets. It knows what callback methods the Servlet has.

请求流程

Client sends HTTP request to Web server Web server forwards that HTTP request to web container. Since Servlet can not understand HTTP, its a Java program, it only understands objects, so web container converts that request into valid request object Web container spins a thread for each request All the business logic goes inside doGet() or doPost() callback methods inside the servlets Servlet builds a Java response object and sends it to the container. It converts that to HTTP response again to send it to the client

容器如何知道哪个Servlet客户端请求了?

有一个文件叫web。xml 这是web容器的主文件 在这个文件中有关于servlet的信息- servlet Servlet-name servlet类 servlet-mappings-类似/Login或/Notifications的路径映射在这里 Servlet-name url模式 等等 web应用程序中的每个servlet都应该在这个文件中有一个条目 查找的方式是- url-pattern -> servlet-name -> servlet-class

如何“安装”servlet ? servlet对象继承自javax.servlet库。*。Tomcat和Spring可以用来利用这些对象来适应用例。

参考-在1.5x上观看- https://www.youtube.com/watch?v=tkFRGdUgCsE。这有一个很棒的解释。

您刚刚得到了一个正常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服务器并访问其功能的标准化方式。

每个Servlet都可以被视为一个小型服务器(因此得名),它可以访问用Java代码建模的请求和响应,以及其他上下文数据,比如Session。

有了这些,servlet的Java代码就可以与它所需要的任何东西交互以呈现响应,包括交给JSP页面来生成HTML视图。

我认为servlet基本上是一个java类,充当HTTP请求和HTTP响应之间的中间路径。Servlet也用来使你的网页动态。例如,如果你想重定向到服务器上的另一个网页,那么你必须使用servlet。另一个重要的事情是servlet既可以在本地主机上运行,也可以在web浏览器上运行。

servlet是当网站用户从服务器请求URL时运行某些功能的Java类。这些函数可以完成将数据保存到数据库、执行逻辑和返回加载页面所需的信息(如JSON数据)等任务。

大多数Java程序使用main()方法在程序运行时执行代码。Java servlet包含doGet()和doPost()方法,它们的作用与main()方法类似。当用户向映射到该servlet的URL发出GET或POST请求时,将执行这些函数。因此,用户可以为GET请求加载页面,或者存储来自POST请求的数据。

当用户发送GET或POST请求时,服务器读取目录中每个servlet类顶部的@WebServlet,以决定调用哪个servlet类。例如,假设你有一个ChatBox类,顶部是这样的:

@WebServlet("/chat")
public class ChatBox extends HttpServlet {

当用户请求/chat URL时,ChatBox类将被执行。