Discussions

Web tier: servlets, JSP, Web frameworks: calling JSP from servlet and passing the data to it.

  1. I am using Javawebserver2.0 and has created a servlet which sets the data in the java bean using set methods.
    It then creates a session and passes this bean object through the ,
    getServletContext().getRequestDispatcher("/new_schedule_add_view.jsp").forward(req,res) method
    to forward the object to the JSP.

    Name of the files
    servlet : new_schedule.java
    JSP file : new_schedule_add_view.jsp
    java bean : new_schedule_add_view.java

    But when I run the servlet and as it tries to call the JSP, it gives the error during the JSP page processing,

    Error : Can not forward to servlet jspServlet : OutputStream or writer has been obtained. Please report this to the administrator of the web server.

    java.lang.IllegalStateException: Can not forward to servlet jspServlet : OutputStream or writer has been obtained at java.lang.Throwable.(Compiled Code) at java.lang.Exception.(Compiled Code) at java.lang.RuntimeException.(RuntimeException.java:50) at java.lang.IllegalStateException.(IllegalStateException.java:48) at com.sun.server.http.HttpRequestDispatcherImpl.forward(HttpRequestDispatcherImpl.java:244) at new_schedule.doPost(new_schedule.java:38) at javax.servlet.http.HttpServlet.service(HttpServlet.java:747) at javax.servlet.http.HttpServlet.service(HttpServlet.java:840) at com.sun.server.ServletState.callService(ServletState.java:226) at com.sun.server.ServletManager.callServletService(ServletManager.java:936) at com.sun.server.http.servlet.InvokerServlet.service(InvokerServlet.java:137) at javax.servlet.http.HttpServlet.service(HttpServlet.java:840) at com.sun.server.ServletState.callService(ServletState.java:226) at com.sun.server.ServletManager.callServletService(ServletManager.java:936) at com.sun.server.ProcessingState.invokeTargetServlet(ProcessingState.java:423) at com.sun.server.http.HttpProcessingState.execute(HttpProcessingState.java:79) at com.sun.server.http.stages.Runner.process(Runner.java:79) at com.sun.server.ProcessingSupport.process(Compiled Code) at com.sun.server.Service.process(Service.java:204) at com.sun.server.http.HttpServiceHandler.handleRequest(HttpServiceHandler.java:374) at com.sun.server.http.HttpServiceHandler.handleRequest(Compiled Code) at com.sun.server.HandlerThread.run(Compiled Code)


    Does anybody knows whether the Javawebserver2.0 supports the RequestDispatcher interface.

    please help, thanks in advance

    -ra
  2. getServletContext().getRequestDispatcher("/new_schedule_add_view.jsp").forward(req,res)


    will throw an Exception if you have already used getOutputStream or getWriter of response.

    This is as per servlet specification.

    Mettu.
  3. Yeah,
    so if you use out.print() before you forward or do somthing like...

     Java scriptlet code...%>
    <HTML>
    <%
    requestDispatcher.forward("");
    ...

    You will get the exception that you mentioned.
    ollie