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

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


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

<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>

<section 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.*/> contain values
corresponding to the target.
</p>

<deftable title="result of invoking calling include(target)">
<tr><td>getXXXX() methods<td>invoking servlet
<tr><td>javax.servlet.include.<td>target
</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>/testapp/index.jsp
<tr><td>request.getContextPath()<td>/testapp
<tr><td>request.getServletPath()<td>/index.jsp
<tr><td>request.getPathInfo()<td>null
<tr><td>request.getQueryString()<td>null
</deftable>

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


<section 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.*/> contain values
corresponding to the invoking servlet.
</p>

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

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

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

<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>/testapp/target.jsp
<tr><td>request.getContextPath()<td>/testapp
<tr><td>request.getServletPath()<td>/target.jsp
<tr><td>request.getPathInfo()<td>null
<tr><td>request.getQueryString()<td>null
</deftable>

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

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

<section 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>/testapp/index.jsp?foo=original
<tr><td>index.jsp contains<td>&lt;jsp:include page="target.jsp"/>
<tr><td>request.getQueryString() in target.jsp returns<td>foo=original
<tr><td>request.getParameter("foo") in target.jsp returns<td>original
<tr><td>request.getAttribute("javax.servlet.include.query_string") in target.jsp returns<td>null
</deftable>

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


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

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

</faq>

<faq title="Class.forName() doesn't seem to work right">
<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>

</faq>

<faq title="package-required" title="Why do I have to put my classes in a package?">
<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>

</faq>

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

<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>


</faq>


</body>
</document>

