Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Dealing with application names in a JEE web app (http://www.velocityreviews.com/forums/t748852-dealing-with-application-names-in-a-jee-web-app.html)

markspace 05-23-2011 07:11 PM

Dealing with application names in a JEE web app
 
Hi all,

I'm delving more heavily into JSP/Servlets and JSF at the moment. I've
found something that looks like a questionable design issue by the JEE
folks at Sun (now Oracle, of course) and I'd like to pick your brains
about how you might deal with it.

Basically, when developing a JEE web app, the application name gets
inserted into every URL and associated path. If my app is named
TechDarwinia, for example, then all URLS look like this:

http://localhost:8080/TechDarwinia/
http://localhost:8080/TechDarwinia/faces/readPost.xhtml
http://localhost:8080/TechDarwinia/rsrc/css/style.css

The problem is of course that the web app could be renamed anything by
the deployer/sysop, and I've got strings hard coded to that app name
TechDarwinia.

So how do folks write their apps so that they can handle being deployed
under different names?

I can think of a couple of solutions, but they don't seem good.

One solution might be to make the app name an application parameter.
This would mean that everywhere I have to insert some sort of EL
variable, or equivalent, and the app takes extra time replacing the
parameter constantly. Also, the installer has one extra parameter to
configure, and I'd like to avoid that.

The app parameter method fails where URLs are required but it's not
possible to invoke Java. For example, in a static resource like CSS:

body {
background:url(/TechDarwinia/rsrc/img/bbg.jpg) left top repeat;
....


Another solution might be, and I haven't investigated this fully, to use
another agent, like Apache, that has the ability to rewrite URLs before
they are presented to the container, inserting the app name wherever it
is lacking. This also requires extra CPU overhead for each request,
(but only one string swap per request, where the app parameter method
may require many EL substitutions), and also extra work by an installer.
This could be very complicated though, as using more than one app at a
time might make the re-write rules overly complicated.

Both cases run the risk of two application names colliding, as there's
no real convention regarding name we apps.


So, has anyone also encountered issues like this? And how did you fix them?

Thanks all!


Lew 05-23-2011 07:30 PM

Re: Dealing with application names in a JEE web app
 
On 05/23/2011 03:11 PM, markspace wrote:
> Hi all,
>
> I'm delving more heavily into JSP/Servlets and JSF at the moment. I've found
> something that looks like a questionable design issue by the JEE folks at Sun
> (now Oracle, of course) and I'd like to pick your brains about how you might
> deal with it.
>
> Basically, when developing a JEE web app, the application name gets inserted
> into every URL and associated path. If my app is named TechDarwinia, for
> example, then all URLS look like this:
>
> http://localhost:8080/TechDarwinia/
> http://localhost:8080/TechDarwinia/faces/readPost.xhtml
> http://localhost:8080/TechDarwinia/rsrc/css/style.css
>
> The problem is of course that the web app could be renamed anything by the
> deployer/sysop, and I've got strings hard coded to that app name TechDarwinia.
>
> So how do folks write their apps so that they can handle being deployed under
> different names?


Relative URLS.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg

markspace 05-23-2011 07:41 PM

Re: Dealing with application names in a JEE web app
 
On 5/23/2011 12:30 PM, Lew wrote:

> Relative URLS.



That seems worse. Now my pages are all dependent on their location in
the directory hierarchy. I like to move things around, especially as I
first start development.

I guess I could constantly refactor as I move things, but I'd like to
avoid that also.


markspace 05-23-2011 08:10 PM

Re: Dealing with application names in a JEE web app
 
On 5/23/2011 12:30 PM, Lew wrote:
> On 05/23/2011 03:11 PM, markspace wrote:
>> So how do folks write their apps so that they can handle being
>> deployed under
>> different names?


> Relative URLS.



I found a use-case where this doesn't work.

Templates are used by various other pages in the web app. They have a
static location, but their links are processed as if the template's
"location" in the directory hierarchy was the location of the template
client.

Example: I have a template at /rsrc/priv/lnf-template.xhtml which
includes other resources. Abbreviated example:

<LNF-TEMPLATE.XHTML>

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<title>
<ui:insert name="title">
Facelets Template
</ui:insert>
</title>
</h:head>

<h:body>
...
</LNF-TEMPLATE.XHTML>

That link href to the style sheet is processed by the client, but the
client could be any page in the directory hierarchy.

For example for a client from the root directory (/readPost.xhtml), that
relative link in the template doesn't work.

<READPOST.XHTML>

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">

<h:body>
<ui:composition template="/rsrc/priv/lnf-template.xhtml" >
Hello from Facelets
</ui:composition>
</h:body>

</html>

</READPOST.XHTML>


Pretty ugly, if you ask me.


Lew 05-23-2011 09:02 PM

Re: Dealing with application names in a JEE web app
 
You left out too much context.

markspace wrote:
> Lew wrote:
>> markspace wrote:
>>> So how do folks write their apps so that they can handle being
>>> deployed under different names?


>> Relative URLS.


> I found a use-case [sic] where this doesn't work.


No, you didn't. I meant relative to the app context, not with stupid "../"
prefixes.

> Templates are used by various other pages in the web app. They have a static
> location, but their links are processed as if the template's "location" in the
> directory hierarchy was the location of the template client.
>
> Example: I have a template at /rsrc/priv/lnf-template.xhtml which includes
> other resources. Abbreviated example:
>
> <LNF-TEMPLATE.XHTML>
>
> <?xml version='1.0' encoding='UTF-8' ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:ui="http://java.sun.com/jsf/facelets"
> xmlns:h="http://java.sun.com/jsf/html">
>
> <h:head>
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
> <link href="../css/style.css" rel="stylesheet" type="text/css" />


What? No!

<link href="/css/style.css" rel="stylesheet" type="text/css" />

Here's a line from one of my own personal facelet projects:
<ui:composition template="/WEB-INF/templates/template.xhtml">

Here are a couple of lines from that template file:

....
<h:outputStylesheet name="/css/purrtones.css" />
</h:head>
<h:body>
<t:div>
<ui:insert name="header">
<h:graphicImage id="logo" value="/images/Duncan_and_the_purrtones.png"
alt="#{bundle.bandname} logo" styleClass="logo"
/>
</ui:insert>
....

All those paths are relative to the deployed application name, which is
"/purrtones" by default, but as you say can change. Thus, the image URL
becomes, e.g.,
http://localhost:8080/purrtones/imag..._purrtones.png
..

> <title>
> <ui:insert name="title">
> Facelets Template
> </ui:insert>
> </title>
> </h:head>
>
> <h:body>
> ...
> </LNF-TEMPLATE.XHTML>
>
> That link href to the style sheet is processed by the client, but the client
> could be any page in the directory hierarchy.
>
> For example for a client from the root directory (/readPost.xhtml), that
> relative link in the template doesn't work.
>
> <READPOST.XHTML>
>
> <?xml version='1.0' encoding='UTF-8' ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:h="http://java.sun.com/jsf/html"
> xmlns:ui="http://java.sun.com/jsf/facelets">
>
> <h:body>
> <ui:composition template="/rsrc/priv/lnf-template.xhtml" >


That would translate to "<application-context>/rsrc/priv/lnf-template.xhtml",
and I am having a hard time correlating that to the CSS example above.

> Hello from Facelets
> </ui:composition>
> </h:body>
>
> </html>
>
> </READPOST.XHTML>
>
>
> Pretty ugly, if you ask me.


I use relative links in templates all the time. But you have to start them
from "/", which translates to the application context root, which is the thing
you're trying not to repeat.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg

Arved Sandstrom 05-23-2011 10:01 PM

Re: Dealing with application names in a JEE web app
 
On 11-05-23 04:11 PM, markspace wrote:
> Hi all,
>
> I'm delving more heavily into JSP/Servlets and JSF at the moment. I've
> found something that looks like a questionable design issue by the JEE
> folks at Sun (now Oracle, of course) and I'd like to pick your brains
> about how you might deal with it.
>
> Basically, when developing a JEE web app, the application name gets
> inserted into every URL and associated path. If my app is named
> TechDarwinia, for example, then all URLS look like this:
>
> http://localhost:8080/TechDarwinia/
> http://localhost:8080/TechDarwinia/faces/readPost.xhtml
> http://localhost:8080/TechDarwinia/rsrc/css/style.css
>
> The problem is of course that the web app could be renamed anything by
> the deployer/sysop, and I've got strings hard coded to that app name
> TechDarwinia.
>
> So how do folks write their apps so that they can handle being deployed
> under different names?

[ SNIP ]

That "application name" is simply the web context root. A J2EE/Java EE
application server can host more than one web application, and that
context root in the request URLs is how the app server knows how to pass
the request to Web App A and not Web App B.

You (anyone in the ops chain) have complete control over what the web
context root is, in the application.xml.

With respect to your complaint about relative URLs, in response to Lew's
suggestion to that effect, I don't think *any* system handles arbitrary
movement of web resources (HTML, JSP, XHTML etc). Regardless of how a
controller parses the resource path described by the URL (URL parts
corresponding to directories in a filesystem hierarchy, or something
different like grabbing resources out of a DB) there still has to be a
definite mapping. This isn't even a J2EE/Java EE thing: it's a web app
thing right across the board.

For your other problem, since the resource locations aren't changing,
and you are using Faces (Facelets), you have access to JSF EL implicit
objects. In a situation like this I use (off the top of my head)

#{facesContext.externalContext.requestContextPath}

so for example

#{facesContext.externalContext.requestContextPath}/rsrc/css/style.css

AHS

Lew 05-24-2011 01:13 AM

Re: Dealing with application names in a JEE web app
 
jebblue wrote:
> Lew wrote:
>> <h:outputStylesheet name="/css/purrtones.css" />


> purrtones? Is this CSS for the late evenings... :)


Duncan is a debonair orange tabby cat, and the PurrTones are his Motown-style
singing group. Mrrreow!

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg

Owen Jacobson 05-24-2011 03:20 AM

Re: Dealing with application names in a JEE web app
 
On 2011-05-23 15:11:32 -0400, markspace said:

> Hi all,
>
> I'm delving more heavily into JSP/Servlets and JSF at the moment. I've
> found something that looks like a questionable design issue by the JEE
> folks at Sun (now Oracle, of course) and I'd like to pick your brains
> about how you might deal with it.
>
> Basically, when developing a JEE web app, the application name gets
> inserted into every URL and associated path. If my app is named
> TechDarwinia, for example, then all URLS look like this:
>
> http://localhost:8080/TechDarwinia/
> http://localhost:8080/TechDarwinia/faces/readPost.xhtml
> http://localhost:8080/TechDarwinia/rsrc/css/style.css
>
> The problem is of course that the web app could be renamed anything by
> the deployer/sysop, and I've got strings hard coded to that app name
> TechDarwinia.
>
> So how do folks write their apps so that they can handle being deployed
> under different names?


<c:url> and equivalent tools that inspect the actual context path (the
"/TechDarwinia/" part of your URL) for the request, largely.

-o


markspace 05-24-2011 04:32 PM

Re: Dealing with application names in a JEE web app
 
On 5/23/2011 2:02 PM, Lew wrote:

> <h:outputStylesheet name="/css/purrtones.css" />



Thanks for pointing this out. I was using a regular <link> attribute.
Those don't seem to be translated or affected by the JSF processing at
all, and just emit their HTML literally. The h:outputStylesheet looks
more sophisticated.

I found a website which explains a bit more:

<http://blogs.oracle.com/rlubke/entry/jsf_2_0_new_feature4>

I've got _Java Server Faces 2.0_, by Ed Burns et al, pub. McGraw-Hill.
Ed Burns was apparently the spec co-lead for JSF 2.0, and I have to say
I'm not impressed by his book. His section on templates uses raw HTML
<link> attributes just like I did, and the whole book barely mentions
h:outputStylesheet. It's only touched on once in the listing of the
entire component library, which is a pretty unpedagogical way of
presenting it.

The Burns book also says that h:outputStylesheet looks for its resources
under the /resources directory in the application root. Since you're
plainly specifying a different location, I'm going to have to explore
resource resolution here a bit more to figure out what the full story is.

Thanks again for getting me pointed in the right direction.



Tom Anderson 05-24-2011 05:16 PM

Re: Dealing with application names in a JEE web app
 
On Mon, 23 May 2011, markspace wrote:

> So how do folks write their apps so that they can handle being deployed
> under different names?


Just to add to what others have said, the ground truth value for the path
at which your app is located is HttpServletRequest.getContextPath:

http://download.oracle.com/javaee/6/...textPath%28%29

Having access to that lets you programmatically construct URLs of various
kinds (absolute, host-relative, etc). That's how various things, like
c:url, get this right.

It doesn't help you deal with this in resources which are not
programmatic, like static CSS, so here you have to depend on relative
references. This doesn't strike me as difficult - you decide early on to
keep all your css under /contextpath/css, and your site furniture images
under /contextpath/images, and then because CSS image paths are relative
to the stylesheet, in your CSS you can write:

background:url(../images/bbg.jpg) left top repeat;

A similar approach should be usable in other static resources which
contain links.

tom

--
Just because Congresspeople do it, doesn't mean it's right. -- Ian York


All times are GMT. The time now is 11:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.