CGI servlet Applet Scriptlet Scriptlet JSP data layer(数据层),business layer(业务层), presentation layer(表现层)

时间:2024-01-03 11:02:44

https://en.wikipedia.org/wiki/Common_Gateway_Interface

In computingCommon Gateway Interface (CGI) offers a standard protocol for web servers to interface with executable programs running on a serverthat generate web pages dynamically. Such programs are known as CGI scripts or simply as CGIs; though usually written in a scripting language, they can be written in any programming language.

http://baike.sogou.com/v87698.htm?fromTitle=servlet

Applet=Application+let
小应用程序

Scriptlet=Script+let
小脚本程序

Servlet=Server+let
小服务程序

Servlet(Server Applet),全称Java Servlet,未有中文译文。是用Java编写的服务器端程序。[1]其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。Servlet运行于支持Java的应用服务器中。从实现上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议Web服务器。最早支持Servlet标准的是JavaSoft的Java Web Server。此后,一些其它的基于Java的Web服务器开始支持标准的Servlet。

https://en.wikipedia.org/wiki/Java_applet

JSPLife.png

CGI   servlet  Applet  Scriptlet Scriptlet JSP  data layer(数据层),business layer(业务层), presentation layer(表现层)

【Servlet Applet】

与 Applet 的比较

相似之处:

* 它们不是独立的应用程序,没有main()方法。

* 它们不是由用户或程序员调用,而是由另外一个应用程序(容器)调用。

* 它们都有一个生存周期,包含init()和destroy()方法。

不同之处:

* Applet具有很好的图形界面(AWT),与浏览器一起,在客户端运行。

* Servlet 则没有图形界面,运行在服务器端。

【Servlet GUI】

Java Servlet 与 CGI(Common Gateway Interface) 的比较:

与传统的CGI和许多其他类似CGI的技术相比,Java Servlet具有更高的效率,更容易使用,功能更强大,具有更好的可移植性,更节省投资。在未来的技术发展过程中,Servlet有可能彻底取代CGI。

在传统的CGI中,每个请求都要启动一个新的进程,如果CGI程序本身的执行时间较短,启动进程所需要的开销很可能反而超过实际执行时间。而在Servlet中,每个请求由一个轻量级的Java线程处理(而不是重量级的操作系统进程)。

在传统CGI中,如果有N个并发的对同一CGI程序的请求,则该CGI程序的代码在内存中重复装载了N次;而对于Servlet,处理请求的是N个线程,只需要一份Servlet类代码。在性能优化方面,Servlet也比CGI有着更多的选择。

* 方便  

Servlet提供了大量的实用工具例程,例如自动地解析和解码HTML表单数据、读取和设置HTTP头、处理Cookie、跟踪会话状态等。

* 功能强大

在Servlet中,许多使用传统CGI程序很难完成的任务都可以轻松地完成。例如,Servlet能够直接和Web服务器交互,而普通的CGI程序不能。Servlet还能够在各个程序之间共享数据,使得数据库连接池之类的功能很容易实现。

* 可移植性好

Servlet用Java编写,Servlet API具有完善的标准。因此,为IPlanet Enterprise Server写的Servlet无需任何实质上的改动即可移植到Apache、Microsoft IIS或者WebStar。几乎所有的主流服务器都直接或通过插件支持Servlet。

* 节省投资  

不仅有许多廉价甚至免费的Web服务器可供个人或小规模网站使用,而且对于现有的服务器,如果它不支持Servlet的话,要加上这部分功能也往往是免费的(或只需要极少的投资)。

【Servlet JSP】

ServletJavaServer Pages(JSP)是一种实现普通静态HTML和动态HTML混合编码的技术,JSP并没有增加任何本质上不能用Servlet实现的功能。但是,在JSP中编写静态HTML更加方便,不必再用println语句来输出每一行HTML代码。更重要的是,借助内容和外观的分离,页面制作中不同性质的任务可以方便地分开:比如,由页面设计者进行HTML设计,同时留出供Servlet程序员插入动态内容的空间。

后来SUN推出了类似于ASP的镶嵌型的JSP,把JSP TAG镶嵌到HTML语句中,这样,就大大简化和方便了网页的设计和修改。新型的网络语言如ASP,PHP,JSP都是镶嵌型的SCRIPT语言。 这是JSP和Servlet区别的运作原理层面。

从网络三层结构的角度看JSP和Servlet的区别,一个网络项目最少分三层:data layer(数据层),business layer(业务层), presentation layer(表现层)。当然也可以更复杂。Servlet用来写business layer是很强大的,但是对于写presentation layer就很不方便。JSP则主要是为了方便写presentation layer而设计的。当然也可以写business layer。写惯了ASP,PHP,CGI的朋友,经常会不自觉的把presentation layer和business layer混在一起。

根据SUN自己的推荐,JSP中应该仅仅存放与presentation layer有关的东东,也就是说,只放输出HTML网页的部份。而所有的数据计算,数据分析,数据库联结处理,统统是属于business layer,应该放在Java BEANS中。通过JSP调用Java BEANS,实现两层的整合。

实际上,微软前不久推出的DNA技术,简单说,就是ASP+COM/DCOM技术。与JSP+BEANS完全类似,所有的presentation layer由ASP完成,所有的business layer由COM/DCOM完成。通过调用,实现整合。

为什么要采用这些组件技术呢?因为单纯的ASP/JSP语言是非常低效率执行的,如果出现大量用户点击,纯SCRIPT语言很快就到达了他的功能上限,而组件技术就能大幅度提高功能上限,加快执行速度。

另外一方面,纯SCRIPT语言将presentation layer和business layer混在一起,造成修改不方便,并且代码不能重复利用。如果想修改一个地方,经常会牵涉到十几页CODE,采用组件技术就只改组件就可以了。

综上所述,Servlet是一个早期的不完善的产品,写business layer很好,写presentation layer就很臭,并且两层混杂。

https://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html

Java Servlet Technology

Shortly after the Web began to be used for delivering services, service providers recognized the need for dynamic content. Applets, one of the earliest attempts toward this goal, focused on using the client platform to deliver dynamic user experiences. At the same time, developers also investigated using the server platform for the same purpose. Initially, Common Gateway Interface (CGI) server-side scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology had many shortcomings, including platform dependence and lack of scalability. To address these limitations, Java Servlet technology was created as a portable way to provide dynamic, user-oriented content.

What Is a Servlet?

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement theServlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.