"Hines, Bill" wrote:
>
> We would like to have our web applications run on different servers with
> perhaps different web application paths. For example, if I set up the web
> app in WAS 3.5 with the web application web path set to /myapp, I have to
> have that name 'myapp' in the webapp descriptor files as well as in any URIs
> in JSP or servlets prefixed to that, even though they are relative. For
> example, in the descriptor file:
>
> <default-page>
>   <uri>/myapp/EntryTableResults.jsp</uri>
> </default-page>

I'm not familiar with WAS, but in a Servlet 2.2 compliant container,
all paths in the Deployment Descriptor (web.xml) are context-releative,
i.e. they do *not* include the context path. The reason for this is to
avoid the exact problem you describe.

> in the JSP:
> <FORM method="POST" action="/myapp/servlet/com.myco.DataEntry"

Since JSP is not tied to a specific markup language, it can not
directly deal with context-relative paths in HTML elements like
this. But there are many ways to handle this so it works no
matter what the context path is:
1) Create a URL mapping for the servlet instead of using the
   "/servlet" prefix (typically mapped to an "invoker"). You can
   then use a path that's relative to the path for the JSP page.
   Say that the JSP page is requested with /myapp/foo/my.jsp.
   If you map the servlet to the context-relative path "/foo/myservlet",
   you can write the HTML like this:

     <form action="myservlet" ...>
2) Develop a custom action that creates the form element and
   translates a context-relative path to an absolute path:

     <mylib:form action="/servlet/com.myco.DataEntry" ...>

3) Use a JSP expression to add the context path:

     <form action="<%= request.getContextPath() %>/servlet/com.myco.DataEntry"
...>

I describe 1) in more detail in my JSP book (<http://TheJSPBook.com/>)
and our InstantOnline Basic (IOB) product contains a custom action for
2) (<http://www.gefionsoftware.com/InstantOnline/>).

> and in any forward/redirect in the servlet, same thing.

<jsp:forward> works with context-relative and page-relative paths, so
there's no need to add the context path (in fact, it's an error to do
so). Redirect requires an absolute path, so you can use the same type
of approach as described above (IOB has a custom action for this as
well).

> Can't the application be coded or the admin set up so that these things
> don't have to be hard-coded into the application source files, so for
> example I could put it on the same server as 'myapp2' easily without having
> to change all of that?

As you can see, it can be handled.

> I was told that this is really a design issue coming from Sun. The Java
> Servlet Specification, v2.2 describes the contextPath as being at the start
> of the URI (chapter 5), and that is what is used to distinguish which web
> app should handle a particular URI. And because of that, the contextPath is
> bound to get into the development code, and that's just the way it is.

That's not correct. All methods in the APIs deal with context-relative
paths (or page/request-relative paths). The only issue is with references
in the markup-language, as described above.

> [...]

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to