I am currently doing an evaluation of JRun 2.3 (build 152) for use on our
project. Our architecture requires the use of the
javax.servlet.RequestDispatcher.forward() method. I have been prototyping using
the forward() method with JRun and have come across the problem that I cannot
forward a request back to the same Servlet (with different parameters). I am not
sure if this is a bug with JRun or my misunderstanding of the Servlet 2.1
specification. Reading the Servlet 2.1 API it states about the forward() method:
You cannot use this method if a ServletOutputStream object or PrintWriter
object has been obtained from the response. In that case, the method throws an
IllegalStateException.
It does not state that a request cannot be forwarded back to the same Servlet.
However JRun is throwing an exception stating that you cannot forward to the
same URI. My questions are:
o Is this a JRun bug or am I completely hosed?
o Does anybody have a work around?
o Why would JRun want to have this restriction? It prevents (valid) recursive
programs.
I have attached the test Servlet I wrote. Interestingly, if you call it with the
URL
"http://localhost/servlet/jruntest.ForwardTest/different?forward=/servlet/jruntest.ForwardTest"
it complains about the same URI even though they are different!
Any insights appreciated,
Oliver
===================== ForwardTest.java ===================================
package jruntest;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
public class ForwardTest
extends javax.servlet.http.HttpServlet
{
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doGet(req, res);
}
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Check if forward
String forward = req.getParameter("forward");
if (forward != null) {
RequestDispatcher rd = getServletConfig().getServletContext()
.getRequestDispatcher(forward);
if (rd == null) {
PrintWriter out = startPage(res);
out.println("Unable to get dispatcher for <B>" + forward + "</B>");
endPage(out);
}
else {
System.err.println("Forwarding to " + forward);
try {
rd.forward(req, res);
}
catch (Exception ex) {
PrintWriter out = startPage(res);
out.println("Failed in forward to <B>" + forward + "</B>");
out.println("when my current URI is " + req.getRequestURI());
out.println("<pre>");
ex.printStackTrace(out);
out.println("</pre>");
endPage(out);
}
System.err.println("Back from forward to " + forward);
}
}
else {
PrintWriter out = startPage(res);
out.println("Request information:");
out.println("<pre>");
print(out, "Request URL", HttpUtils.getRequestURL(req).toString());
print(out, "Request URI", req.getRequestURI());
print(out, "Servlet path", req.getServletPath());
print(out, "Query string", req.getQueryString());
out.println("</pre>");
Enumeration e = req.getParameterNames();
if (e.hasMoreElements()) {
out.println("<h1>Servlet parameters (Single Value style):</h1>");
out.println("<pre>");
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println(" " + name + " = " + req.getParameter(name));
}
out.println("</pre>");
}
endPage(out);
}
}
private void print(PrintWriter out, String name, String value)
throws IOException
{
out.print(" " + name + ": ");
out.println(value == null ? "<none>" : value);
}
private void print(PrintWriter out, String name, int value)
throws IOException
{
out.print(" " + name + ": ");
if (value == -1) {
out.println("<none>");
}
else {
out.println(value);
}
}
private PrintWriter startPage(HttpServletResponse res)
throws IOException
{
res.setContentType("text/html");
final PrintWriter out = res.getWriter();
out.println("<html><head>");
out.println("<title>ForwardTest Output</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>ForwardTest Output</h1>");
return out;
}
private void endPage(PrintWriter out)
{
out.println("</body></html>");
}
public String getServletInfo()
{
return "A servlet that tests request forwarding";
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html