且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

com.sap.ui5.resource.ResourceServlet的工作原理介绍

更新时间:2022-09-02 13:07:56

There is one question asking how and where the js file “resources/sap-ui-core.js” is loaded when you run your UI5 application locally ( for example using tomcat )?


In my sample project mymap, there is no folder named resources and thus no sap-ui-core.js either.

com.sap.ui5.resource.ResourceServlet的工作原理介绍In order to figure out what happens in the runtime, let’s have a look at the web.xml under folder WEB-INF in the project.


There is a ResourceServlet defined. Actually it is the responsibility of this servlet delivered by SAP, which returns the content of resources like js, css and other type in the runtime. So now if I would like to investigate on this servlet, how could I get its source code?

com.sap.ui5.resource.ResourceServlet的工作原理介绍How to get source code of ResourceServlet

Suppose you have already an working Tomcat instance. Right click your UI5 project, choose Export->War file, and manually copy that exported war file to the webapps folder of your tomcat instance folder. In my case the folder is : C:\myProgram\tomcat-7.0.54\webapps.

Now start your tomcat via bat file, in my case: “C:\myProgram\tomcat-7.0.54\bin\startup.bat”:

You should see one information message that the war file is deployed:

com.sap.ui5.resource.ResourceServlet的工作原理介绍com.sap.ui5.resource.ResourceServlet的工作原理介绍After that you could find three .class file in the unzipped folder, in my case they are:


(1) “C:\myProgram\tomcat-7.0.54\webapps\mymap\WEB-INF\lib\com.sap.ui5.resource_1.24.1\com\sap\ui5\resource\ResourceServlet.class”


(2) “C:\myProgram\tomcat-7.0.54\webapps\mymap\WEB-INF\lib\com.sap.ui5.resource_1.24.1\com\sap\ui5\resource\impl\ServletResource.class”


(3) “C:\myProgram\tomcat-7.0.54\webapps\mymap\WEB-INF\lib\com.sap.ui5.resource_1.24.1\com\sap\ui5\resource\impl\ServletResourceLocator.class”

The last step, google “jd-gui” and download it. It allows you to directly review source code of a .class file.

com.sap.ui5.resource.ResourceServlet的工作原理介绍More investigation on ResourceServlet

The main job of resource handling is wrapped in method serveResource of class ResourceServlet. We could find at least 2 useful hint from this method.


(1) use dev mode to figure out where the resource is loaded

com.sap.ui5.resource.ResourceServlet的工作原理介绍You could directly browse your application resource by appending “/resources/” to your application url, in my case it is: http://localhost:8080/mymap/resources/

com.sap.ui5.resource.ResourceServlet的工作原理介绍You might already notice the “CLASSPATH”, what does it mean?


The constructor of ServletResource which extends base class Resource has one parameter source, which indicates whether this resource is loaded locally or remotely ( configured through parameter com.sap.ui5.resource.REMOTE_LOCATION ):

com.sap.ui5.resource.ResourceServlet的工作原理介绍com.sap.ui5.resource.ResourceServlet的工作原理介绍com.sap.ui5.resource.ResourceServlet的工作原理介绍com.sap.ui5.resource.ResourceServlet的工作原理介绍