Solved

Application-level varibale?

Posted on 2002-05-19
11
211 Views
Last Modified: 2012-05-04
    I have controller.asp which parses a XML file. All http requests come to the controller.asp with a request type. (i.e All the forms' action attribute is equal to controller.asp in all pages with a hidden parameter for the request type). The controller.asp parses the XML file and based on the reqeuest type, gets a ASP Name from the XML file and transfers request to that ASP.  This is the scenario.

     In controller.asp, I am using the Microsoft XML DOM component to enable parsing. (Code Below). Since the XML DOM is created in the controller.asp, for every request, the DOM object is created, xml file loaded, parsed and destroyed, which is surely a performance overhead!

     I am wondering if I can carry out the first two activities - (i.e Creating the XML DOM object and loading the XML file) once in the start of the application and just use this object in the controller.asp, then I am reducing the object creating and loading XML file overhead for the every request. right? Please let me know your opinions.

     If right, I need some help with implementing it.

     One way, is in the Application_OnStart eventhandler in global.asa file. I can create a application-level varibable set to the XML DOM object and load the XML file in the application-level variable. I can just use this application-level variable in the controller.asp to parse ! How is this implementation pattern? I have always heard and read that COM objects must not be put in Session. Considering this, can I put the XML DOM object (which is COM object) as a application level variable?

     Seeking opinions!

******************************************************
<%    

     Dim requestType
     Dim xmlDoc
     dim NNPObj
     dim docElement
     dim nodeObj
     dim docObj
     dim childNode
     dim rootNode
     dim cNodes


     requestType=request.form("requestType")
     
     set xmlDoc=server.createobject("Microsoft.XMLDOM")
     xmlDoc.async=false

     dim bool

     bool=xmlDoc.load(server.mappath("MyInfo.xml"))



     set docElement=xmlDoc.documentElement
     set cNodes= docElement.childNodes

     for each nodeObj in cNodes
         set NNPObj =nodeObj.attributes


         
                      if NNPObj.getNamedItem("id").nodeValue=requestType then
          response.write "Request Type: "+ requestType +"<br><hr>"
          response.write " <b><u>Model Type</u></b>: "+ NNPObj.getNamedItem("modelType").nodeValue +"<br><br>"

          if NNPObj.getNamedItem("modelType").nodeValue="asp" then

               response.write " <b><u>Processing ASP </u></b>: "+ NNPObj.getNamedItem("ASPName").nodeValue +"<br><hr>"

          elseif NNPObj.getNamedItem("modelType").nodeValue="com" then          

               response.write "<b><u>Component Name</u></b>: "+ NNPObj.getNamedItem("componentName").nodeValue +"<br>"
               response.write "<b><u>Method Name</u></b> :"+ NNPObj.getNamedItem("methodName").nodeValue +"<br>"
               response.write "<b><u>Presentation Asp</u></b>: "+ NNPObj.getNamedItem("presentationAsp").nodeValue +"<br><hr>"

          end if
                       end if
     Next

%>
*****************************************************
XMl File:

<controller>
     <action id="getEmployee"
          modelType="asp"
          ASPName="getEmployee.asp"
     />
     <action id="addEmployee"
          modelType="asp"
          ASPName="addEmployee.asp"
     />
     <action id="generateOppurunity"
          modelType="com"
          componentName="SANGenSoft.Oppurunity"
          methodName="generateOppurunity()"
          presentationAsp="confirmOppurunity.asp"
     />
     <action id="showOppurunity"
          modelType="com"
          componentName="SANGenSoft.Oppurunity"
          methodName="showOppurunity()"
          presentationAsp="showOppurunity.asp"
     />


</controller>
***************************************************
0
Comment
Question by:rajivraj
  • 8
11 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 450 total points
ID: 7019511
This is what you should do.

global.asa
===========
<object
 id="oPersist"
 progid="Microsoft.FreeThreadedXMLDom"
 runat="server"
 scope="application"></object>
<script language="VBScript" runat="server">

Sub Application_OnStart()
 oPersist.async = False
 oPersist.load Server.MapPath("MyInfo.xml")
End Sub

</script>

page.asp
========
<%

 Dim oXML
 'Setting it to the Application Scoped Variable
 Set oXML = Application.StaticObjects("oPersist")
 
%>

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7019530
Notes
=====
1. The way you are using XML and the DOM is not going to allow for an optimised performance-oriented outcome. In short, it is a clear abuse of the XML Process.

What will follow is the correct way of using XML to squeeze the most out of performance.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7019538
MSXML Parser That Should Be Used:
==================================
MSXML Version 3.0 SP 2 (Recommended MSXML Version 4.0)

controller.xsl
===============
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:param name="requestType" />
<xsl:template match="/">
<xsl:apply-templates select="controller/action[@id [. = $requestType]]" />
</xsl:template>
<xsl:template match="action">
Request Type: <xsl:value-of select="@id" /><br /><hr />
<xsl:choose>
<xsl:when test="@modelType[ . = 'asp']">
<xsl:call-template name="asp" />
</xsl:when>
<xsl:when test="@modelType[ . = 'com']">
<xsl:call-template name="com" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="asp">
<b><u>Processing ASP</u></b>: <xsl:value-of select="@ASPName" /><br /><hr />
</xsl:template>
<xsl:template name="com">
<b><u>Component Name</u></b>: <xsl:value-of select="@componentName" /><br />
<b><u>Method Name</u></b>: <xsl:value-of select="@methodName" /><br />
<b><u>Presentation Asp</u></b>: <xsl:value-of select="@presentationAsp" /><br /><hr />
</xsl:template>
</xsl:stylesheet>

global.asa
==========
<object id="oTemplate"
 progid="Msxml2.XSLTemplate.3.0"
 runat="server"
 scope="application"></object>
<object id="oPersist"
 progid="Msxml2.FreeThreadedDOMDocument.3.0"
 runat="server"
 scope="application"></object>
<script language="VBScript" runat="server">
 Sub Application_OnStart()
  Dim oXSLT
  Set oXSLT = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
 oXSLT.async = False
 oXSLT.load Server.MapPath("controller.xsl")
  Set oTemplate.stylesheet = oXSLT
  Set oXSLT = Nothing
  oPersist.async = False
  oPersist.load Server.MapPath("controller.xml")
 End Sub

</script>


page.asp
========
<%@language="VBScript"%>
<%
 On Error Resume Next
 Dim oTemplate, oProc, oPersist
 Set oTemplate = Application.StaticObjects("oTemplate")
 'get the IXSLProcessor whose rental model
 'is suited for the application scope
 Set oProc = oTemplate.createProcessor()
 Set oPersist = Application.StaticObject("oPersist")
 With oProc
   .input = oPersist
   .addParameter "requestType",Request("requestType")
   .transform()
   
   Response.Write(.output)
 End With
 Set oProc = Nothing
 Set oPersist = Nothing
 Set oTemplate = Nothing

%>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7019544
What's going on
===============
1. We can store the stylesheet in application scope directly using the Msxml2.FreeThreadedDOMDocument.3.0 progid but it is too heavy in resource management plus it is not designed for fast performance. How? If you have includes, it will not be loaded until at transform time, thus having a strong impact on transforms

2. Microsoft thus, created the IXSLTemplate and IXSLProcessor which is targetted especially in cases such as yours.

3. The problem with your code is that you will have to loop multiple times to do your work in your ASP Code. The XSLT Document takes care of that. More importantly, because of the rental model, it is lightweight. To create the IXSLProcessor, the following must be true.
The stylesheet used must be FreeThreaded. The IXSLTemplate's stylesheet property must be set to that of the FreeThreaded XSLT Document.

4. After getting the the IXSLProcessor, you can then set the input property to that of the Cached XML Document. This way will completely eliminate any I/O processes as every is taking place from memory.

5. By using the XML/XSLT Combination, the ASP code is leaner and meaner.



0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7019563
If you do not wish to change your code and want to use the older MSXML Parser, then you just change the global.asa and change the asp code to the following:
<%
Dim szRequestType
Dim oXML
Dim oNode
szRequestType = Request.Form("requestType")
Set oXML = Application.StaticObjects("oPersist")
Set oNode = oXML.selectSingleNode("/controller/action[@id[. = '" & szRequestType & "']]")
If Not oNode Is Nothing Then
  Response.Write "Request Type: " & szRequestType  & "<br><hr>"
  Response.Write "<b><u>Model Type</u><b>: " & oNode.getAttribute("modelType") & "<br><br>"
  Select Case oNode.getAttribute("modelType")
  Case "asp"
    Response.Write "<b><u>Processing ASP</u></b>: " & oNode.getAttribute("ASPName") & "<br><hr>"    
  Case "com"
    Response.Write "<b><u>Component Name</u></b>: " & oNode.getAttribute("componentName") & "<br>"
    Response.Write "<b><u>Method Name</u></b>: " & oNode.getAttribute("methodName") & "<br>"
    Response.Write "<b><u>Presentation Asp</u></b>: " & oNode.getAttribute("presentationAsp") & "<br><hr>"
  End Select
  Set oNode = Nothing
End If
Set oXML = Nothing
%>
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 23

Expert Comment

by:b1xml2
ID: 7019564
by the way the amendment with regard to the controller.xsl, adding the model type display =)

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:param name="requestType" />
<xsl:template match="/">
<xsl:apply-templates select="controller/action[@id [. = $requestType]]" />
</xsl:template>
<xsl:template match="action">
Request Type: <xsl:value-of select="@id" /><br /><hr />
Modal Type :<xsl:value-of select="@modelType" /><br /><br />
<xsl:choose>
<xsl:when test="@modelType[ . = 'asp']">
<xsl:call-template name="asp" />
</xsl:when>
<xsl:when test="@modelType[ . = 'com']">
<xsl:call-template name="com" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="asp">
<b><u>Processing ASP</u></b>: <xsl:value-of select="@ASPName" /><br /><hr />
</xsl:template>
<xsl:template name="com">
<b><u>Component Name</u></b>: <xsl:value-of select="@componentName" /><br />
<b><u>Method Name</u></b>: <xsl:value-of select="@methodName" /><br />
<b><u>Presentation Asp</u></b>: <xsl:value-of select="@presentationAsp" /><br /><hr />
</xsl:template>
</xsl:stylesheet>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7019569
to download MSXML3 SP2, go to http://msdn.microsoft.com/xml and click on the right link MSXML3 SP2

if you wish to use MSXML4 SP1, click on the right link and read the install instructions. In the case of MSXML4, the version id should be changed from .3.0 to .4.0 in all cases presented here.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7019912
agrees with b1xml2. It IS the recommended way. although I personally hate the overload of the application. Especially if all xml output is save din the application scope, but that's just me :-/

CJ
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7022119
rajivraj, feedback pls...
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8836427
No comment has been added lately and it seems that this question have been abandoned. So it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question or invite a Moderator to close this question if there's no reply from you after seven days.

In the absence of responses, I will recommend the following:

To accept the comment and points awarded to b1xml2

** PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER **


Just trying to help for the cleanup...
gladxml
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now