Web Applications FAQ
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Overview
Deploy
Tutorials
FAQ
Scrapbook
Tutorials
Web Applications
Scrapbook

  1. How are path related values of the request object changed after a forward or include?
    1. include
    2. forward
    3. Exception: getNamedDispatcher()
    4. Request parameters
  2. Class.forName() doesn't seem to work right
  3. Why do I have to put my classes in a package?
  4. How do I specify the default file encoding?

How are path related values of the request object changed after a forward or include?

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?

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

include

When a servlet (or jsp) has been invoked by an include, the path related methods of the request object correspond to the invoking servlet. Request attributes javax.servlet.include.* contain values corresponding to the target.

result of invoking calling include(target)
getXXXX() methodsinvoking servlet
javax.servlet.include.target

/testapp/index.jsp
<jsp:include page="target.jsp"/>

path related methods in target.jsp after include
request.getRequestURI()/testapp/index.jsp
request.getContextPath()/testapp
request.getServletPath()/index.jsp
request.getPathInfo()null
request.getQueryString()null

request attributes available in target.jsp
request.getAttribute("javax.servlet.include.request_uri")/testapp/target.jsp
request.getAttribute("javax.servlet.include.context_path")/testapp
request.getAttribute("javax.servlet.include.servlet_path")/target.jsp
request.getAttribute("javax.servlet.include.path_info")null
request.getAttribute("javax.servlet.include.query_string")null

forward

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

result of invoking calling forward(target)
getXXXX() methodstarget
javax.servlet.forward.invoking servlet

From the discussion of forward() in the Servlet specification:

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

/testapp/index.jsp?foo=bar
<jsp:forward page="target.jsp"/>

path related methods in target.jsp after forward
request.getRequestURI()/testapp/target.jsp
request.getContextPath()/testapp
request.getServletPath()/target.jsp
request.getPathInfo()null
request.getQueryString()null

request attributes available in target.jsp
request.getAttribute("javax.servlet.forward.request_uri")/testapp/index.jsp
request.getAttribute("javax.servlet.forward.context_path")/testapp
request.getAttribute("javax.servlet.forward.servlet_path")/index.jsp
request.getAttribute("javax.servlet.forward.path_info")null
request.getAttribute("javax.servlet.forward.query_string")null
caucho.forwardtrue

Exception: getNamedDispatcher()

If getNamedDispatcher() is used to obtain a dispatcher to do the include() or forward(), no attributes are set.

Request parameters

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

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.

include index.jsp
called with url/testapp/index.jsp?foo=original
index.jsp contains<jsp:include page="target.jsp"/>
request.getQueryString() in target.jsp returnsfoo=original
request.getParameter("foo") in target.jsp returnsoriginal
request.getAttribute("javax.servlet.include.query_string") in target.jsp returnsnull

include index.jsp?foo=override
called with url/testapp/index.jsp?foo=original
index.jsp contains<jsp:include page="target.jsp?foo=override"/>
request.getQueryString() in target.jsp returnsfoo=original
request.getParameter("foo") in target.jsp returnsoverride
request.getAttribute("javax.servlet.include.query_string") in target.jsp returnsfoo=override

forward index.jsp
called with url/testapp/index.jsp?foo=original
index.jsp contains<jsp:foward page="target.jsp"/>
request.getQueryString() in target.jsp returnsnull
request.getParameter("foo") in target.jsp returnsoriginal
request.getAttribute("javax.servlet.forward.query_string") in target.jsp returnsfoo=original

forward index.jsp?foo=override
called with url/testapp/index.jsp?foo=original
index.jsp contains<jsp:foward page="target.jsp?foo=override"/>
request.getQueryString() in target.jsp returnsfoo=override
request.getParameter("foo") in target.jsp returnsoverride
request.getAttribute("javax.servlet.forward.query_string") in target.jsp returnsfoo=original

Class.forName() doesn't seem to work right

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.

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.

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

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

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

Why do I have to put my classes in a package?

If I make a class that is not in a package, Resin does not recognize it and does not load it.

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.

The solution is to put all of your classes in a package.

See http://java.sun.com/j2se/1.4/compatibility.html, point #8, second bullet point.

How do I specify the default file encoding?

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

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

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

A simple JSP can be used to determine the default encoding that is being used:

file-encoding.jsp
<%@ page session="false" import="java.io.*" %>

System.getProperty("file.encoding"): 
<%= System.getProperty("file.encoding") %>

<p>

OutputStreamWriter encoding is: 
<%= (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding() %>

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

OutputStreamWriter encoding is: ISO8859_1


Tutorials
Web Applications
Scrapbook
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.