我读了很多文章来理解Java servlet,但我没有成功。

你能简单介绍一下Java servlet吗(用简单的语言)?什么是servlet?它的优点是什么?

我不能理解服务器端编程语言(PHP, ASP)和servlet之间的区别。


当前回答

Servlet是一个java类,用于响应HTTP请求并生成HTTP响应......当我们使用HTML创建页面时,它将是一个静态页面,因此为了使其动态,我们使用SERVLET{简单地说,人们可以理解}。 JSP克服了利用servlet的问题,它本身就使用了代码和HTML标记。

其他回答

Servlet是一个java类,用于响应HTTP请求并生成HTTP响应......当我们使用HTML创建页面时,它将是一个静态页面,因此为了使其动态,我们使用SERVLET{简单地说,人们可以理解}。 JSP克服了利用servlet的问题,它本身就使用了代码和HTML标记。

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是一个不错的主意。这是一个较慢的学习过程,但对弄清基本知识更有帮助。

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

什么是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。这有一个很棒的解释。

Java servlet是处理和响应客户需求并实现servlet接口的服务器端Java程序模块。它有助于提高Web服务器的有用性,而开销、维护和支持可以忽略不计。

servlet充当客户和服务器之间的中介。由于servlet模块一直在服务器上运行,它们可以获取客户的需求并对其做出反应。servlet的Demand和reaction对象提供了一种有用的方法来处理HTTP请求并将内容信息发送回客户。

由于servlet是与Java方言协调的,因此它还具有Java的所有优点,例如,高可移动性、阶段自主权、安全性和Java数据库可用性。