File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes clarification regarding RequestDispatcher usage Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "clarification regarding RequestDispatcher usage" Watch "clarification regarding RequestDispatcher usage" New topic
Author

clarification regarding RequestDispatcher usage

Prakash Moorthy
Greenhorn

Joined: Aug 03, 2011
Posts: 6
Hi

am calling a jsp(chk.jsp) from a html file(branch.html) by the html form action, then in chk.jsp forwarding the requset to the branch.html page by using requestDispatcher

RequestDispatcher view = request.getRequestDispatcher("branch.html");
view.forward(request, response);

am getting the following error,

org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:421)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:608)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:200)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:189)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:122)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:79)
org.apache.jsp.chk_jsp._jspService(chk_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:73)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:378)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

but if i use the <jsp:forward page="branch.html" />, then its working fine, and it's also working fine, if i use the branch.html as branch.jsp

please clarify.

Thanks.
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 85
http://www.coderanch.com/t/289883/JSP/java/IllegalStateException-getOutputStream-has-already-been
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

please post your jsp code for more clarity


Ashwin Sridhar
SCJP | SCWCD | OCA
Prakash Moorthy
Greenhorn

Joined: Aug 03, 2011
Posts: 6
Hi Ashwin

the jsp code

<%@ page import="com.work.bean.Branch, java.io.*, java.util.*" %>

<%!
ArrayList<Branch> bList = new ArrayList<Branch>();
%>

<%

String prevAction = (String)session.getAttribute("prevAction");

if(prevAction == null || !prevAction.equals("add"))
bList.clear();

String action=request.getParameter("saction");

session.setAttribute("prevAction",action);

Branch br = new Branch();
br.setName(request.getParameter("bname"));
System.out.println("bname ="+request.getParameter("bname")+"\n");
bList.add(br);

session.setAttribute("bList",bList);

if(action.equals("add")){
RequestDispatcher view = request.getRequestDispatcher("branch.html");
view.forward(request, response);
}
%>

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56150
    
  13

That code should be in a servlet, not a JSP.

Java code in a JSP has been considered a poor practice and discredited for almost 10 years noes because of issues such as this (among many other good reasons).

Your first step is to refactor your application to use servlets and JSPs appropriately. You may find this article helpful.

And, please UseCodeTags when posting code to the forums.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://jrebel.com/download
 
subject: clarification regarding RequestDispatcher usage
 
Similar Threads
Can't include other page content with RequestDispatcher.include()
RequestDispatcher question
RequestDispatcher showing error??
getOutputStream() has already been called for this response
Opening a zip file from JSP page