• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Websphere 3.5 and Request Dispatcher (Part 2)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please excuse the second post on the same problem, only this code should describe it far better.....
Further refinement of the problem has lead us to produce the following servlet to test our thoery...
Does anyone have any clues as to why you cannot run an RequestDispatcher.include and a RequestDispatcher.forward within the same servlet ???
The following code demonstrates this...
We can call it with includeOnly as many times as we like, and likewise for forwardOnly, but if we call 'both', we get the following output after the code.
Anyone come across this before???
public class ForwardServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**
* Service the request
*/
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (request.getParameter("operation").equalsIgnoreCase("both")) {
ServletContext ctx = this.getServletContext();
RequestDispatcher rd1 = ctx.getNamedDispatcher("DoNothingServlet");
RequestDispatcher rd2 = ctx.getRequestDispatcher("/test.jsp");
rd1.include(request, response);
rd2.forward(request, response);
} else if (request.getParameter("operation").equalsIgnoreCase("includeOnly")) {
RequestDispatcher rd1 = this.getServletContext().getNamedDispatcher("DoNothingServlet");
rd1.include(request, response);
} else if (request.getParameter("operation").equalsIgnoreCase("forwardOnly")) {
RequestDispatcher rd2 = this.getServletContext().getRequestDispatcher("/test.jsp");
rd2.forward(request, response);
}
}
Error 500
An error has occured while processing request:http://www.wlp.itouch.co.uk/ForwardServlet
Message: Server caught unhandled exception from servlet [ForwardServlet]: Server caught unhandled exception from servlet [jsp]: null
Target Servlet: ForwardServlet
StackTrace:
--------------------------------------------------------------------------------
Root Error-1: null
java.lang.NullPointerException
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:874)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:626)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:160)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:287)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:105)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:360)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:775)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:701)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:478)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:234)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at uk.co.itouch.whitelabel.servlet.ForwardServlet.service(ForwardServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:626)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:160)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:287)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:105)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:360)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:775)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:701)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:478)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:234)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:77)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:160)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:300)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp$ServiceRunnable.run(SQEventListenerImp.java:230)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp.notifySQEvent(SQEventListenerImp.java:104)
at com.ibm.servlet.engine.oselistener.serverqueue.SQEventSource.notifyEvent(SQEventSource.java:212)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.notifyService(SQWrapperEventSource.java:353)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.run(SQWrapperEventSource.java:220)
at com.ibm.servlet.engine.oselistener.outofproc.OutOfProcThread$CtlRunnable.run(OutOfProcThread.java:248)
at java.lang.Thread.run(Thread.java:481)
--------------------------------------------------------------------------------
Wrapped Error-2: null
javax.servlet.ServletException
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:376)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:775)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:701)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:478)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:234)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at uk.co.itouch.whitelabel.servlet.ForwardServlet.service(ForwardServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:626)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:160)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:287)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:105)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:360)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:775)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:701)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:478)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:234)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:77)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:160)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:300)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp$ServiceRunnable.run(SQEventListenerImp.java:230)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp.notifySQEvent(SQEventListenerImp.java:104)
at com.ibm.servlet.engine.oselistener.serverqueue.SQEventSource.notifyEvent(SQEventSource.java:212)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.notifyService(SQWrapperEventSource.java:353)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.run(SQWrapperEventSource.java:220)
at com.ibm.servlet.engine.oselistener.outofproc.OutOfProcThread$CtlRunnable.run(OutOfProcThread.java:248)
at java.lang.Thread.run(Thread.java:481)
--------------------------------------------------------------------------------
Wrapped Error-3: Server caught unhandled exception from servlet [jsp]: null
com.ibm.servlet.engine.webapp.UncaughtServletException: Server caught unhandled exception from servlet [jsp]: null
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:376)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:775)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:701)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:478)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:234)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:77)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:160)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:300)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp$ServiceRunnable.run(SQEventListenerImp.java:230)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp.notifySQEvent(SQEventListenerImp.java:104)
at com.ibm.servlet.engine.oselistener.serverqueue.SQEventSource.notifyEvent(SQEventSource.java:212)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.notifyService(SQWrapperEventSource.java:353)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.run(SQWrapperEventSource.java:220)
at com.ibm.servlet.engine.oselistener.outofproc.OutOfProcThread$CtlRunnable.run(OutOfProcThread.java:248)
at java.lang.Thread.run(Thread.java:481)
--------------------------------------------------------------------------------
Wrapped Error-4: Server caught unhandled exception from servlet [ForwardServlet]: Server caught unhandled exception from servlet [jsp]: null
com.ibm.servlet.engine.webapp.UncaughtServletException: Server caught unhandled exception from servlet [ForwardServlet]: Server caught unhandled exception from servlet [jsp]: null
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:230)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:138)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:77)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:160)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:300)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp$ServiceRunnable.run(SQEventListenerImp.java:230)
at com.ibm.servlet.engine.oselistener.SQEventListenerImp.notifySQEvent(SQEventListenerImp.java:104)
at com.ibm.servlet.engine.oselistener.serverqueue.SQEventSource.notifyEvent(SQEventSource.java:212)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.notifyService(SQWrapperEventSource.java:353)
at com.ibm.servlet.engine.oselistener.serverqueue.SQWrapperEventSource$SelectRunnable.run(SQWrapperEventSource.java:220)
at com.ibm.servlet.engine.oselistener.outofproc.OutOfProcThread$CtlRunnable.run(OutOfProcThread.java:248)
at java.lang.Thread.run(Thread.java:481)
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a response is committed, you cannot forward a request. By committed, I mean the output stream is obtained from the response.
I am not sure what you are trying to do? It doesn't make any sense to do an include and a forward. You can do multiple includes, as long as the JSP or servlets you include don't forward.
If you provide some detail about what it is that you need to do maybe I can help...
Cheers
 
Robin M
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What I have been doing for around a year now is using what is referred to as a DispatcherServlet. This servlet takes care of including the processing of x number of servlets and then forwarding to an appropriate view. This is documented in a number of places and works great on JRun and Tomcat, just not on Websphere 3.5.4. (Except once, every time the Websphere is restarted or a new jar file deployed, which is really strange. It fails to work a second time giving the above error msg...)
The idea is that you pass the dispatcher servlet a list of servlets (either via querystring or form and also an appropriate view to forward to). This removes the tight coupling between the servlets and view and allows you to pass the output view as a parameter).
This design also also you to design small reusable servlets that can be 'included' at any time to perform small/often repeated tasks.
The code sample submitted above was merely developed as a way of debugging the goings on in Websphere and to demonstrate the problem.....
Hope this clear up my explanation, any ideas ?
 
Erick Jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robin,
The DispatcherServlet architecture is certainly a common one, and is also the one we use. I have never tried to do an include and a forward, but in thinking about it, it still does not make sense to me.
If you include a servet/JSP, then you are committing the response to send back to the client. When you use the request dispatcher, the output stream is flushed before including the resource. Therefore, you cannot forward.
I assume that the controller servlet that you include does not write to the output stream? It seems to me that if other application servers let you do this, then they did not follow the spec (although, I can not point you where in the spec this is prohibited).
I am not sure if my answer is clear so let me state it another way. Say in your servlet, you access the output stream by calling response.getOutputStream(). After that call the response is committed. You will get an exception if you try to forward. The RequestDispatcher.include accesses the output stream to flush it, so I am assuming this is the exception that you are getting. I normally see an exception message like "response already committed".
Hope this helps!
Erick
 
Robin M
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Erick,
I wasn't aware that the include method commited the output. I actually made a post under the servlets message board also as I thought I had a different problem, although it may appear that these 2 problems are the same. Take a look....
https://coderanch.com/t/354865/Servlets/java/ReequestDispatcher-include
Can you please give me a code example of how you would achieve the same thing as I am attempting then....something you use perhaps. That would be great !
I'm amazed that this has been running under tomcat and jrun...
 
Robin M
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have been using for some time.....
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
String[] controllers = request.getParameterValues("controller");
String view = request.getParameter("view");
String origView = request.getParameter("origView");
RequestDispatcher rd = null;
java.lang.Exception exception = null;
log("origView=" + origView);
log("view=" + view);
// validate required parameters have been supplied.
if (controllers == null || view == null) {
PrintWriter out = response.getWriter();
out.write("Dispatcher servlet expects at least one controller parameter and a view parameter.");
out.flush();
return; // stop further processing immediately
}
try {
// call each controller in the order specified, verifying that each does indeed exist.
// existence is determined from the servlet having been registered in the deployment descriptor
for (int i = 0; i < controllers.length; i ++) {
rd = getServletContext().getNamedDispatcher(controllers[i]);
if (rd == null) {
throw new ServletException("Controller " + controllers[i] + " does not exist");
} else {
log("Including Cont Servlet : " + controllers[i]);
}
rd.include(request, response);
}
// forward to the appropriate view appending any querystring info that might have been added to the request object
QueryString qString = (QueryString)request.getAttribute("queryString");
if (qString != null) {
view = view + qString.toString();
}
rd = getServletContext().getRequestDispatcher(view);
rd.forward(request, response);
} catch (ServletException ex) {
QueryString qString = (QueryString)request.getAttribute("queryString");
// don't allow null values for qString object.
if (qString == null) qString = new QueryString();
log("loc + qString = " + origView + qString);
if (ex.getRootCause() instanceof InvalidParameterException || ex.getRootCause() instanceof MandatoryFileMissingException) {
rd = getServletContext().getRequestDispatcher(origView + qString);
log("forwarding to origView");
} else {
log("forwarding to errorView");
rd = getServletContext().getRequestDispatcher(errorView + qString);
}
// add the exception as an attribute in the request object for retrieval by the errorView
request.setAttribute("myException", ex);
rd.forward(request, response);
} catch (RuntimeException rex) {
// caters for NullPointerExceptions and such like.

QueryString qString = (QueryString)request.getAttribute("queryString");
// don't allow null values for qString object.
if (qString == null) qString = new QueryString();
rd = getServletContext().getRequestDispatcher(errorView + qString);
// add the exception as an attribute in the request object for retrieval by the errorView
request.setAttribute("myException", rex);
rd.forward(request, response);
}
}
 
Robin M
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you take a look at this link
http://java.sun.com/docs/books/tutorial/servlets/communication/request-dispatcher.html
The last example where an include is made actually writes to the outputstream suggesting it is not committed doesn't it ?
 
Erick Jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robin,
This example is ok because there is no forward after the include. You can certainly write directly to the output stream after an include, you just can't forward.
I am leaving on vacation for the week, so I won't be able to respond again until next week. Sorry.
Good Luck!
Erick
 
Robin M
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Erik,
Thanks for all yopur help on this one.
I have got round the problem another way, a way that I think is actually a lot better as you can dynamically extend the web app without registering new servlets. Here is a link to the article I read if anyone is interested.
A very good way of using MVC in Web App design
http://www.martinfowler.com/isa/frontController.html
Again, thanks for your help.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic