• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error: JSP response has been committed already.

 
Balaji Gadam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont use forward to many times... response can be commited only once...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic