JSP Newbie:

I have a piece of code that works perfectly within the <% %> brackets but I need it to work within the <%! %> brackets.

Here is the code:
------------------------

com.newatlanta.cfc.CFCProxy myCFC = new com.newatlanta.cfc.CFCProxy("xmpp", request, response);
		
		    String jsonMsgData = "A test message";
		    Object[] myArgs = {jsonMsgData};
		
		    java.util.Map map = (java.util.Map)myCFC.invoke("OnAvailabilityChange", myArgs);

-------------------------

I am working with XMPP / Jabber using Smack and I need for this piece of code to be triggered when there is a change in user presence. I already have the trigger code working within the <%! %> brackets and just need to plug this piece in it to be executed. Right now, what I have the trigger code doing is sending me an IM of the user presence. I want to replace the "IM message code" with the above code to be executed.

Obviously the code above is some sort of servlet and I know it works for sure -BUT ONLY- within the <% %> brackets as mentioned previously. Is there a way I can call that piece of code a as a function like doCFCProxy(arg1, arg2...); into the trigger code (that is surrpunded by <%! %>)?

Anything placed in the JSP between the <%! %> tags [jsp declaration tag] is placed outside the service() method but inside the auto-generated servlet class. You can of course create a method containing the given piece of code, which can be invoked from within the code placed inside the <% %> [scriptlet tags]

The bottom line is that anything placed in the JSP declaration section forms part of the auto-generated servlet [and is *not* executed] whereas anything placed between the JSP scriptlet tags is executed for every request received by that JSP. In your case, you might want to declare a method in the declaration section and invoke the same from a scriptlet.
.

<%!
private static void invokeSomething() {
    // your stuff here
}
%>

<%
invokeSomething();
%>

Oh, and in case you didn't know, placing logic in JSP's and using scriptlets is bad and has been deprecated since times unknown.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.