Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Error: JSP response has been committed already. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Error: JSP response has been committed already." Watch "Error: JSP response has been committed already." New topic
Author

Error: JSP response has been committed already.

Balaji Gadam
Greenhorn

Joined: Oct 02, 2009
Posts: 1
Hello All,

I am pretty much new to this forum. Please reply me for my question..

I have been asked this question in interview, I might have got this problem many times but I forgot exactly what could be the reason.
Question is: Error Message - Response hasbeen already committed. When will I get this error?

I read in some forums and just tried to replicate the same. But, still I havent get any exception and successfully running..

So, please someone give me any example to regenerate the issue and the solution to resolve. Thanks in Advance friends..

Here is the example I coded:
SampleServlet.java
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

session = request.getSession(true);
if(i==1){
session.setAttribute("a","a1");
session.setAttribute("b","b1");
session.setAttribute("c","c1");
}

getServletContext().getRequestDispatcher("/JSP/consumerHomeWatchList.jsp").forward(request, response);
}

consumerHomeWatchList.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'consumerHomeWatchList.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page1">

</head>

<body>
<%= "A: " + session.getAttribute("a") + ", B:" + session.getAttribute("b") + ", C: " + session.getAttribute("c")%> <br>
<% request.setAttribute("from", "consumer");
request.getRequestDispatcher("/JSP/MyJsp.jsp").forward(request, response);
%>
</body>
</html>

Calling the servlet and forwarded succesfully to my jsp without any error. please help me in this..

Thanks,
Balaji.G
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
You might get this error when you try to forward the same request more than once,

Since the first forward is already committed,next one is not allowed and hence the error.

Try adding another forward and see what happens.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
pankaj vijay
Ranch Hand

Joined: Apr 01, 2008
Posts: 75
Dont use forward to many times... response can be commited only once...


Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error: JSP response has been committed already.
 
Similar Threads
Posting session variables in single JSP page
Why dissimilar output for the same jsp code in the tomcat6 server?
passing multiple values from one page to another
the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is
JSP Not Reading Arraylist