Sign in

E-mail *, (xx@domain.com)
Password *

Register | Forgot password

Displaying a (really) variable variable on an otherwise cached page

The "WCB: JSP front-end design" forum contains threads on issues that are related to JSP front-end design while programming a WCB (WebManager Component Bundle). The Presentation & Element component contain JSPs that will be used for front-end rendering. Ask your questions related to these JSPs here. Some example topics: integration of AJAX, use of the taglibs, how to handle multiple websites, and so forth.

Answered
Not marked as answered yet

Forums  >  WCB: JSP Front-end design  >  Displaying a (really) variable variable on an otherwise cached page


Author Displaying a (really) variable variable on an otherwise cached page
Cthulhu



Posts: 212

Posted: 09-06-2009 10:46

Yeah. I've been trying to get the ID of the currently logged in user (extracted from the session) to display on a WebManager page, using the following snippet:


<%
HttpSession session = request.getSession(true);
String userData = (String) session.getAttribute("GX_user");

if (userData != null) {
int start = userData.indexOf("<id>");
int end = userData.indexOf("</id>");

if (start != -1 && end != -1) {
String userId = userData.substring(start, end);
this.getServletContext().setAttribute("userid", userId);
}
}

%>

<c:if test="${not empty userid}">
User ID: ${userid}
<% response.getOutputStream().print(userId); %>
</c:if>
<c:if test="${empty userid}">
No user ID set, are you logged in?
</c:if>



This works, BUT, once the user ID variable has been set, it won't change into something else again (tested in this case by logging out - the page still says the current user's ID is '3'). I'm pretty sure that this is due to the caching system - i.e. a page and the replacement fields aren't regenerated for each request.

So the question is: How would I get the user ID to be regenerated and placed into the JSP's output properly each time? I've also tried the following:



response.getOutputStream().print(userId);



but that (for some reason) results in an undetailed 500 error. For the record, errors without a proper reason are irritating.

So, what's another way of doing this? I tried to look it up on Google, but I couldn't find anything, :/.

williamb



Posts: 310

Posted: 09-06-2009 13:23 Helpful

While I was reading the upper half of your message I was thinking "Would the cache allow this? As the page itself doesn't change, the JSP wouldn't be rendered again...".
Reading the rest of the question confirmed my thoughts: that is caching!

Why not add a personalization expression here like <wm-userid> and use personalizations to render your data here?

martinvm



Posts: 59

Posted: 09-06-2009 13:54 Helpful

Hi Cthulhu

It is not recommended to use Java snippets in JSPs (as is stated in the Design API manual) because their behavior is unpredictable in environments with heavy caching. JSPs are only rendered again if the timestamp expires, which is not on every request.
If you need truly dynamic calls you have to use other methods such as personalization or filters or a combination of both.

Cthulhu



Posts: 212

Posted: 09-06-2009 17:36

I'll go give those personalization expressions a look at, thanks. And yeah, I guessed as much about the caching and JSP snippets, .

patricka



Posts: 280

Posted: 09-06-2009 17:54 Helpful

It just so happens that I was training this topic today!

The document "Personalization Toolkit" is your friend. Also, take a peek at the XMLdebug info to find out the XPATH expression to the userid.

In short: I'm guessing that making a "select" expression for "/root/system/user/userid", tagging it as "wm-cthulhu-loves-userid" and typing a tag <wm-cthulhu-loves-userid/> somewhere on the page would do the trick.

Greetings,

Patrick

Cthulhu



Posts: 212

Posted: 10-06-2009 09:35

Yeah, that looks like it'll do what I'm looking for. Just a few more questions, bear with me ^^.

* Is it possible to add this personalization through a WCB's code? You know, so the WCB will work out of the box.

* Is there (again, in code) a neater way to get the current user's ID than to manually parse or extract the ID from the HttpSession? Can that xquery be executed on some API-available object to get the same results?


Edit: Yeah, that'll do the trick, =D. Thanks you guys.

patricka



Posts: 280

Posted: 11-06-2009 13:40 Solved

Tough questions! While pondering the answer, my roommate stumbled upon the following bit of code in the wmpcommunityedition xslStyleSheet.jspf:

<c:forEach var="presentation" items="${website.presentations}">
<c:if test="${presentation.scopes[0] == 'WebSite' &&
fn:startsWith(presentation.identifier,'xslStyleSheetWCB')}">
<wm:render presentationName="${presentation.identifier}" />
</c:if>
</c:forEach>



A hook!

This means that if you start the name of your presentation with "xslStyleSheetWCB" and give it scope "WebSite", it will be picked up and rendered in the general stylesheet.


See, personalization is all about getting the expressions in the XSL stylesheet. This hook will allow you to create a presentation and have it embedded in the XSL stylesheet. E.g. like below:

<!-- chtulhuLovesUserid.xml -->
<presentation>
<name>xslStyleSheetWCB Cthulhu Loves Userid</name>
<display-name>Cthulhu Loves Userid</display-name>
<scope>WebSite</scope>
</presentation>



and

<xsl:template match="wm-cthulhu-loves-userid">
<xsl:value-of select="/root/system/user/userid"/>
</xsl:template>



And elsehwere you would put that tag to good use in your favorite .jspf.

As for the second question: I don't think there is a better way to determine the userid, because your code will probably not be called at all because of caching. If you want to execute code and be sure of the userid, use personalization to determine it and use that id in either a server side include (SSI) or an AJAX call.

Greetings,

Patrick

Back to top

New message: "Displaying a (really) variable variable on an otherwise cached page"
Message:
bold boitalicd underline url quote code smile cool eek grin mad razz sad wink
 
© 2012 GX Software B.V.

Disclaimer

This website (Connect.gxsoftware.com) may discuss or contain opinions, (sample) coding, software or other information that does not include GX official interfaces, instructions or guidelines and therefore is not supported by GX. Changes made based on this information are not supported.  GX will not be held liable for any damages caused by using or misusing the information, software, instructions, code or methods suggested on this website, and anyone using these methods does so at his/her own risk. GX offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this website, including any liability resulting from incompatibility between the content of this website and the materials and services offered by GX. By using this website you will not hold, or seek to hold, GX responsible or liable with respect to the content of this website.