ABAP SICF服务和Java Servlet的比较

In my opinion ABAP ICF handler and Java Servlet play the same role in enhancement which enables your web server with additional functionality.

This blog will not introduce how an ICF handler class in ABAP or a Servlet in Java are developed, but focus the way those instances of handler class or Servlet are spawned by Web Server.

Let’s first study the Servlet spawn behavior in Java.

Servlet in Java

According to Servlet specification, http request against a given url will be served by the same single instance of servlet.

For example, I have developed a simple Servlet which just returns “Hello world” as response. I map it with url starting with “/Hello”.

In the doGet method, I print out the current thread id and instance address to try to verify if every request is served with the SAME servlet instance.

System.out.println("User id: " + userId + " at thread: " + Thread.currentThread().getId() + " current instance: " + this);

In client side I use jQuery to send out five different request simultaneously:

var LOCAL = "http://localhost:9098/JerryServlet/Hello?userId=";
var PREFIX = "i04241";
function main() {
 for( var i = 0; i < 5; i++) {
 var url = LOCAL + PREFIX + i;
 var html = getPostByAJAX(url);
 console.log("response: " + html);
 }
}
$(function(){
    main();
});
function getPostByAJAX(requestURL){
 var html = $.ajax({
    url: requestURL, async: true}).responseText;
 return html;
}

When executing the client JavaScript code, I observe in Server console and could find out that these five requests are handled by the same Servlet instance ,however in different threads.

Since I perform the request in an asynchronous mode, so the response of those five requests are processed and returned in parallel.

How singleton behavior of Servlet is achieved

The instance of requested Servlet will only be initialized when it is asked for the first time. The below two-fold instance checking against null is a typical thread-safe implementation pattern for Singleton in Java.

The method loadServlet in class StandardWrapper will call constructor of my sample Servlet via reflection. All subsequent request will be served by this singleton.

Thread pool in Java Servlet

This thread pool behavior is easy to observe, just set breakpoint in doGet method in my Servlet, line 58:

Perform the client code to send five requests, then in Eclipse we can see the five working threads stopped at the breakpoint at the same time:

From the bottom of callstack we get the hint that those working threads are centrally managed by the Thread pool.

ICF Handler class in ABAP

Create a simple ICF service in tcode SICF and implement its handler class.

Set a breakpoint on method HANDLE_REQUEST, then trigger the request sending in client code:

Five debugger windows will pop up for the same time, each for one separate ABAP session where the request is handled.

From this source code we know the fact that in ABAP, the instance of handler class does not behave as singleton in Java Servlet.

From debugging we can observe that different session has different ICF handler instance which are isolated among each other.

Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:

  • Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP
  • Functional programming – Simulate Curry in ABAP
  • Functional Programming – Try Reduce in JavaScript and in ABAP
  • Simulate Mockito in ABAP
  • A simulation of Java Spring dependency injection annotation @Inject in ABAP
  • Singleton bypass – ABAP and Java
  • Weak reference in ABAP and Java
  • Fibonacci Sequence in ES5, ES6 and ABAP
  • Java byte code and ABAP Load
  • How to write a correct program rejected by compiler: Exception handling in Java and in ABAP
  • An small example to learn Garbage collection in Java and in ABAP
  • String Template in ABAP, ES6, Angular and React
  • Try to access static private attribute via ABAP RTTI and Java Reflection
  • Local class in ABAP, Java and JavaScript
  • Integer in ABAP, Java and JavaScript
  • Covariance in Java and simulation in ABAP
  • Various Proxy Design Pattern implementation variants in Java and ABAP
  • Tag(Marker) Interface in ABAP and Java
  • Bitwise operation ( OR, AND, XOR ) on ABAP Integer
  • ABAP ICF handler and Java Servlet
  • ADBC and JDBC
  • CL_ABAP_CORRESPONDING, CL_JAVA_CORRESPONDING and CL_JS_CORRESPONDING
  • Build an Cross Site Scripting example in Java and ABAP
  • Play around with JSONP in nodeJS server and ABAP server

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

原文地址:https://www.cnblogs.com/sap-jerry/p/10114047.html

时间: 2024-11-06 03:41:00

ABAP SICF服务和Java Servlet的比较的相关文章

JavaWeb之Java Servlet完全教程(转)

Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servlet必须部署在Java servlet容器才能使用.虽然很多开发者都使用Java Server Pages(JSP)和Java Server Faces(JSF)等Servlet框架,但是这些技术都要在幕后通过Servlet容器把页面编译为Java Servlet.也就是说,了解Java Servle

Java Servlet系列之Servlet生命周期

Servlet生命周期定义了一个Servlet如何被加载.初始化,以及它怎样接收请求.响应请求,提供服务.在讨论Servlet生命周期之前,先让我们来看一下这几个方法: 1. init()方法 在Servlet的生命周期中,仅执行一次init()方法,它是在服务器装入Servlet时执行的,可以配置服务器,以在启动服务器或客户机首次访问Servlet时装入Servlet.无论有多少客户机访问Servlet,都不会重复执行init(): 2. service()方法 它是Servlet的核心,每当

java Servlet接口及应用(转)

基本类和接口 一.javax.servlet.Servlet接口 servlet抽象集是javax.servlet.Servlet接口,它规定了必须由Servlet类实现由servlet引擎识别和管理的方法集.Servlet接口的基本目标是提供生命期方法init().service()和destroy()方法. servlet接口中的方法       void init(ServletConfit config)throws ServletException    在servlet被载入后和实施

java Servlet接口及应用

基本类和接口 一.javax.servlet.Servlet接口 servlet抽象集是javax.servlet.Servlet接口,它规定了必须由Servlet类实现由servlet引擎识别和管理的方法集.Servlet接口的基本目标是提供生命期方法init().service()和destroy()方法. servlet接口中的方法       void init(ServletConfit config)throws ServletException    在servlet被载入后和实施

Java Servlet 总结

Servlet是一个接口,接口中有5个方法:init.service.destroy.getServletInfo.getServletConfig.对于Servlet来说,生命周期就是init,service,destroy三步,其中service可能会调用多次. GenericServlet实现了Servlet接口,它只是通用的Servlet,和协议无关. HttpServlet继承了GernericServlet,增加了doGet.doPost等方法,需要开发者去复写,默认动作是报错.在s

Java Servlet解析

Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servlet必须部署在Java servlet容器才能使用.虽然很多开发者都使用Java Server Pages(JSP)和Java Server Faces(JSF)等Servlet框架,但是这些技术都要在幕后通过Servlet容器把页面编译为Java Servlet.也就是说,了解Java Servle

Java Servlet完全教程

Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servlet必须部署在Java servlet容器才能使用.虽然很多开发者都使用Java Server Pages(JSP)和Java Server Faces(JSF)等Servlet框架,但是这些技术都要在幕后通过Servlet容器把页面编译为Java Servlet.也就是说,了解Java Servle

J2EE 13规范(3)-Java Servlet

Servlet简介: 一个servlet就是Java编程语言中的一个类,它被用来扩展服务器的性能,服务器上驻留着可以通过"请求-响应"编程模型来访问的应用程序.虽然servlet可以对任何类型的请求产生响应,但通常只用来扩展Web服务器的应用程序.Java Servlet技术为这些应用程序定义了一个特定于HTTP的 servlet类. javax.servlet和javax.servlet.http包为编写servlet提供了接口和类.所有的servlet都必须实现Servlet接口,

Java Servlet工作原理问答

导读 本文来自stackoverflow的问答,讨论了Java Servlet的工作机制,如何进行实例化.共享变量和多线程处理. 问题:Servlet是如何工作的?Servlet 如何实例化.共享变量.并进行多线程处理? 假设我有一个运行了大量 Servlet 的 web 服务器.通过 Servlet 之间传输信息得到 Servlet 上下文,并设置 session 变量. 现在,如果有两名或更多使用者向这个服务发送请求,接下来 session 变量会发生什么变化?究竟是所有用户都是用共同的变量