Steal The Fish

...e gli ruba il pesce.

Andrea Dessì  //  Andrea, Man, about 20, (x)html operator, web lover.
I'm a Entando Developer!

Jan 9 / 7:45am

Use Java webapp context for web-html issues

Hypothesis:

We have a wonderful java web application which is reachable from:

And you have to setup css and js scripts, something like

<link href="path/sitestyle.css" media="screen" type="text/css" />

and

<script type="text/javascript" src="path/mootools.js"><script>

But the problem is: how to write the correct path using an absolute path? (relative path sucks).

Why absolute path: I really love absolute path because when using struts with actions you can really go crazy.

So in your JSPs using the standard JSTL

<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>

try to use this:

<c:set var="currentPath">http://<%= request.getServerName()+":"+request.getServerPort()+request.getContextPath() %></c:set>

so this variable "currentPath" will be rendered as http://internet.externalhost.com:80/myapp if users are coming from internet, rendered as http://intranethostname/myapp if visiting from lan.

The html maybe should be:

<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<c:set var="currentPath">http://<%= request.getServerName()+":"+request.getServerPort()+request.getContextPath() %></c:set>
<html>
<head>
<link href="<c:out value="${currentPath}" />/css/sitestyle.css" media="screen" type="text/css" />
<script type="text/javascript" src="<c:out value="${currentPath}" />/js/mootools.js"><script>
</head>
<body>
[ ... ]
</body>
</html>
Filed under  //  jsp   tips  

Comments (0)