Jesús Zazueta
Jesús Zazueta

Reputation: 1192

Getting the absolute context path of a webapp inside a Velocity template

I have a fairly simple (I hope :P) question.

I know that in good old Java servlet code, I can derive the full request path of a request that comes into the web server like this:

String requestBaseUrl = 
    String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), 
            request.getServerPort(), request.getContextPath());

Now, doing the exact same thing in JSP is pretty straightforward to do. However, I now have to do this in Velocity 1.5 template. Specifically, I need to output that information as the 'codebase' attribute of a webstart applet I need to integrate into my web application.

I tried this, but the applet refused to start, instead throwing a FileNotFoundException :P

<script src="http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = { code:'org.groundhog.MyApplet', width:360, height:280} ;
    var parameters = {
        jnlp_href: './webstart/myapplet.jnlp', <------------------ this
        bgColor: '#4f616d', mayscript: true
    };
    deployJava.runApplet(attributes, parameters, '1.6');

So I think that the most reliable way to do this is to derive and stick in the absolute URL where the application is running, and reference the applet's jnlp file from there using the same logic as in my previous Java snippet.

Right now I'm shuffling through Velocity's documentation. But, if anyone has had to deal with something similar, I'd be thankful for the equivalent idiom in Velocity.

I thank you for your time and help!

Upvotes: 0

Views: 2802

Answers (1)

Jes&#250;s Zazueta
Jes&#250;s Zazueta

Reputation: 1192

Well, after a short mind-clearing break, I came to the realization that essentially, Velocity is just a text template engine, and should have no business in doing code trickery in its views. Matt's comment just confirmed my suspicions ;). Thank you!

The simplest thing to do was to first derive the code base URL in my controller object, and then put that into the template's model (say, a variable called "codeBaseUrl"), and just let Velocity render it. In short, a combination of the two approaches above.

I offer my apologies, since I once again realize that 8 hours of sleep can solve nearly any problem. :P

Thank you for your time.

Upvotes: 1

Related Questions