Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding request disptacher

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
Thnkx for the reply
but can u pls tell me why it will not work with getServletContext()
and why will it work with implicit application object
u also suggested to use <jsp:forward>
can it be used with requestdispatcher also?
[ April 10, 2002: Message edited by: Gaurav Chikara ]
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Gaurav !
The following code doesn't work :
<%@ page language="java" import="javax.servlet.http.*" %>
<%
RequestDispatcher rp = getServletContext().getRequestDispatcher("/source.jsp");
rp.forward(request,response);
%>
It generates following errors :
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:159)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)
at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:176)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:198)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:193)
at org.apache.jsp.testReqDispatch$jsp._jspService(testReqDispatch$jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it says undefined class or variable and gives the compiler error
What compiler error?
then i tried
this.getServletContext.getRequestDispatcher("/relativepath.jsp").forward.(request,response);

Rather than calling getServletContext(), use the implicit "application" object: application.getRequestDispatcher("path")... Also, the path you show here isn't relative!
It gave Internal Server Error
Surely it gave more information? An exception stack trace? An error code or message?
can anyone pls tell how to get and use request dispatcher in jsp
Is there any reason why you don't want to use <jsp:forward/> ?
- Peter
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code is working :
<%@ page session="true" %>
<%@ page language="java" import="javax.servlet.http.*" %>

<%
RequestDispatcher rp = getServletContext().getRequestDispatcher("/jsp/Disp.jsp");
rp.forward(request,response);
%>
But if I put an 'out.println("Something");' statement before forwarding , its not getting printed, whats the reason ? Even after calling forward() method, the 'out.println' doesnt print anything ? Pls. clarify...
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the spec or javadoc for this method -- the first thing forward() does is flush the response buffer. When it returns, the response is committed and closed; any attempt to write to the output stream should throw an IOException.
If you call forward() when the response has already been committed, an IllegalStateException is thrown.
- Peter
 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
Thnkx for the reply
but can u pls tell me why it will not work with getServletContext()
and why will it work with implicit application object
u also suggested to use <jsp:forward>
can it be used with requestdispatcher also?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will work with getServletContext() but why do this if the JSP makes an "application" variable available for the purpose?
I suggested <jsp:forward> for the same reason: why bother explicitly rummaging around with RequestDispatcher if there's an easy to use tag available for exactly that purpose? The JSP compiler will turn this tag into a RequestDispatcher.forward() call, so there's really no difference between them.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic