<document>
<header>
  <product>resin</product>
  <title>Web Applications FAQ</title>
  <description>
  </description>
</header>

<body>
<summary objsummary="localtoc"/>


<s1 name="forward-path" title="How are path related values of the request object changed after a forward or include?" type="faq">

<description>
When I have a jsp "index.jsp" and it does a forward to "target.jsp",
request.getRequestURI() returns the path for "target.jsp". 
How can I get the value for the original uri?
</description>

<p>
The values of getRequestURI() and other path related method's change for a
forward(), but remain the same for an include().  
</p>

<s2 title="include">
<p>
When a servlet (or jsp) has been invoked by an include, the path related
methods of the <code>request</code> object correspond to the invoking
servlet.  Request attributes <code>javax.servlet.include.*</code> contain values
corresponding to the target.
</p>

<deftable title="result of invoking calling include(target)">
<tr><td>getXXXX() methods</td><td>invoking servlet
</td></tr><tr><td>javax.servlet.include.</td><td>target
</td></tr></deftable>

<example title="/testapp/index.jsp">
&lt;jsp:include page="target.jsp"/&gt;
</example>

<deftable title="path related methods in target.jsp after include">
<tr><td>request.getRequestURI()</td><td>/testapp/index.jsp
</td></tr><tr><td>request.getContextPath()</td><td>/testapp
</td></tr><tr><td>request.getServletPath()</td><td>/index.jsp
</td></tr><tr><td>request.getPathInfo()</td><td>null
</td></tr><tr><td>request.getQueryString()</td><td>null
</td></tr></deftable>

<deftable title="request attributes available in target.jsp">
<tr><td>request.getAttribute("javax.servlet.include.request_uri")</td><td>/testapp/target.jsp
</td></tr><tr><td>request.getAttribute("javax.servlet.include.context_path")</td><td>/testapp
</td></tr><tr><td>request.getAttribute("javax.servlet.include.servlet_path")</td><td>/target.jsp
</td></tr><tr><td>request.getAttribute("javax.servlet.include.path_info")</td><td>null
</td></tr><tr><td>request.getAttribute("javax.servlet.include.query_string")</td><td>null
</td></tr></deftable>
</s2>


<s2 title="forward">

<p>
When a servlet (or jsp) has been invoked by a forward, the path related
methods of the <code>request</code> object return the path of the target
servlet.  Request attributes <code>javax.servlet.forward.*</code> contain values
corresponding to the invoking servlet.
</p>

<deftable title="result of invoking calling forward(target)">
<tr><td>getXXXX() methods</td><td>target
</td></tr><tr><td>javax.servlet.forward.</td><td>invoking servlet
</td></tr></deftable>

<p>From the discussion of forward() in the Servlet specification:</p>

<blockquote>
The path elements of the request object exposed to the target servlet must
reflect the path used to obtain the RequestDispatcher.
</blockquote>

<example title="/testapp/index.jsp?foo=bar">
&lt;jsp:forward page="target.jsp"/&gt;
</example>

<deftable title="path related methods in target.jsp after forward">
<tr><td>request.getRequestURI()</td><td>/testapp/target.jsp
</td></tr><tr><td>request.getContextPath()</td><td>/testapp
</td></tr><tr><td>request.getServletPath()</td><td>/target.jsp
</td></tr><tr><td>request.getPathInfo()</td><td>null
</td></tr><tr><td>request.getQueryString()</td><td>null
</td></tr></deftable>

<deftable title="request attributes available in target.jsp">
<tr><td>request.getAttribute("javax.servlet.forward.request_uri")</td><td>/testapp/index.jsp
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.context_path")</td><td>/testapp
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.servlet_path")</td><td>/index.jsp
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.path_info")</td><td>null
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.query_string")</td><td>null
</td></tr><tr><td>caucho.forward</td><td>true
</td></tr></deftable>
</s2>

<s2 title="Exception: getNamedDispatcher()">
<p>
If getNamedDispatcher() is used to obtain a dispatcher to do the include() or forward(), no attributes are set.
</p>
</s2>

<s2 title="Request parameters">

<p>A call to forward() or include() can contain parameters in the url.  These
parameters are returned with a subsequent call to request.getParameter().
</p>

<p>The value of request.getQueryString(), however, depends on whether an
include() or a forward() was done, and you may end up with the bizarre
situation where the value of request.getParameter() does not match the value in
the query string.  </p>

<deftable title="include index.jsp">
<tr><td>called with url</td><td>/testapp/index.jsp?foo=original
</td></tr><tr><td>index.jsp contains</td><td>&lt;jsp:include page="target.jsp"/&gt;
</td></tr><tr><td>request.getQueryString() in target.jsp returns</td><td>foo=original
</td></tr><tr><td>request.getParameter("foo") in target.jsp returns</td><td>original
</td></tr><tr><td>request.getAttribute("javax.servlet.include.query_string") in target.jsp returns</td><td>null
</td></tr></deftable>

<deftable title="include index.jsp?foo=override">
<tr><td>called with url</td><td>/testapp/index.jsp?foo=original
</td></tr><tr><td>index.jsp contains</td><td>&lt;jsp:include page="target.jsp?foo=override"/&gt;
</td></tr><tr><td>request.getQueryString() in target.jsp returns</td><td>foo=original
</td></tr><tr><td>request.getParameter("foo") in target.jsp returns</td><td>override
</td></tr><tr><td>request.getAttribute("javax.servlet.include.query_string") in target.jsp returns</td><td>foo=override
</td></tr></deftable>


<deftable title="forward index.jsp">
<tr><td>called with url</td><td>/testapp/index.jsp?foo=original
</td></tr><tr><td>index.jsp contains</td><td>&lt;jsp:foward page="target.jsp"/&gt;
</td></tr><tr><td>request.getQueryString() in target.jsp returns</td><td>null
</td></tr><tr><td>request.getParameter("foo") in target.jsp returns</td><td>original
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.query_string") in target.jsp returns</td><td>foo=original
</td></tr></deftable>

<deftable title="forward index.jsp?foo=override">
<tr><td>called with url</td><td>/testapp/index.jsp?foo=original
</td></tr><tr><td>index.jsp contains</td><td>&lt;jsp:foward page="target.jsp?foo=override"/&gt;
</td></tr><tr><td>request.getQueryString() in target.jsp returns</td><td>foo=override
</td></tr><tr><td>request.getParameter("foo") in target.jsp returns</td><td>override
</td></tr><tr><td>request.getAttribute("javax.servlet.forward.query_string") in target.jsp returns</td><td>foo=original
</td></tr></deftable>
</s2>

</s1>

<s1 title="Class.forName() doesn't seem to work right" type="faq">
<description>
When I do Class.forName(classname).getInstance() I do not seem to a newly
loaded Class Object. It seems that I always just get the cached old version
and not a new version if I compile a new Class into the classpath.
</description>

<p>
Class.forName(String) has a bit of an obscure feature, it uses the "defining
classloader of the current class" when it searches for the class.
</p>

<p>
To successfully accomplish the functionality of Class.forName() 
in all circumstances, you can use:
</p>


<example>
private Class classForName(String classname)
{
  ClassLoader loader = Thread.currentThread().getContextClassLoader();

  if (loader != null)
    _theClass = Class.forName(classname, false, loader);
  else
    theClass = Class.forName(classname);
}
</example>

</s1>

<s1 title="Why do I have to put my classes in a package?" type="faq">
<description>
If I make a class that is not in a package, Resin does not recognize it and 
does not load it.
</description>

<p>
Starting with Java 1.4 the use of classes that are not in a package (sometimes
called the "default" package") is deprecated.  Java classes should always be in
a package.  In some version of Java, packageless classes are not going to be
supported at all.
</p>

<p>
The solution is to put all of your classes in a package.
</p>

<p>
See
<a href="http://java.sun.com/j2se/1.4/compatibility.html">http://java.sun.com/j2se/1.4/compatibility.html</a>, 
point #8, second bullet point.
</p>

</s1>

<s1 title="How do I specify the default file encoding?" type="faq">

<description>
I want to specify the default file encoding for InputStreamReader and
OutputStreamReader, using the system property <code>file.encoding</code>, but
it doesn't seem to work.  
</description>

<p>
The default file encoding for the JVM is set with the system property
<code>file.encoding</code>.  The JDK will only pick up that system property if
it is specified at startup:
</p>

<example>
$ bin/httpd -Dfile.encoding=ISO-8859-1
</example>

<p>
A simple JSP can be used to determine the default encoding that is being used:
</p>

<example title="file-encoding.jsp">
&lt;%@ page session="false" import="java.io.*" %&gt;

System.getProperty("file.encoding"): 
&lt;%= System.getProperty("file.encoding") %&gt;

&lt;p&gt;

OutputStreamWriter encoding is: 
&lt;%= (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding() %&gt;
</example>

<results>
System.getProperty("file.encoding"): ISO-8859-1

OutputStreamWriter encoding is: ISO8859_1
</results>


</s1>


</body>
</document>
