java.lang.IllegalStateException: Cannot forward after response has been committed

java.lang.IllegalStateException: Cannot forward after response has been committed

Hey I am new to Jsp/Servlets i am getting following Exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:349)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at servlets.Controller.doGet(Controller.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

and my Servlet Code is

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

 

 

import Beans.Registry;


/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller" )
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Controller() {
// super();
// TODO Auto-generated constructor stub
}

 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();

String services = request.getParameter("Services");
String username = request.getParameter("UserName");
String password = request.getParameter("Password");
System.out.println("username>>>>>>>>>>>>>>>>>>>>>>"+username);
System.out.println("Services>>>>>>>>>>>>>>>>>>>>>>"+services);
System.out.println("Password>>>>>>>>>>>>>>>>>>>>>>"+password);
System.out.println("Requested Services:"+services);
Registry reg = new Registry();

//String user =(String)request.getSession().getAttribute("UserName");

request.getSession().setAttribute("UserName", username);
request.setAttribute("Password", password);


if(username==null&&(services==null|| (!services.equals("login")))){
String err ="Unauthorized Access";
request.setAttribute("errormsg", err);
RequestDispatcher disp =getServletContext().getRequestDispatcher("/error.jsp");
disp.forward(request, response);
return;


}else{
try{
if(!reg.find(services)){
String err ="Unreconized Services";
request.setAttribute("errormsg", err);
RequestDispatcher disp = getServletContext().getRequestDispatcher("/error.jsp");
disp.forward(request,response);
return;


}else{
System.out.println("Service Class:"+reg.getsClass());
String cl="Beans."+reg.getsClass();
Class cls = Class.forName(cl);
Object obj = cls.newInstance();
Method mth = cls.getMethod(reg.getsMethod(), HttpServletRequest.class,HttpServletResponse.class);
String redirectPage = (String)mth.invoke(obj, request,response);
System.out.println("Return Page:"+redirectPage);

RequestDispatcher disp =getServletContext().getRequestDispatcher(redirectPage);
disp.forward(request, response);
//on this foward request i am getting the above exception;
return;

}

}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
out.close();

}
}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

It is properly excuting viewUser method
the following code
/**
*
*/
package Beans;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.RequestDispatcher;
import javax.xml.ws.Response;

import org.apache.catalina.connector.Request;

import connection.Database;

/**
*
*
*/
public class UserClass {

private Number User_ID;
private String User_Name;
private String Password;
private String User_Type;

 

public Number getUser_ID() {
return User_ID;
}
public void setUser_ID(Number user_ID) {
User_ID = user_ID;
}
public String getUser_Name() {
return User_Name;
}
public void setUser_Name(String user_Name) {
User_Name = user_Name;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getUser_Type() {
return User_Type;
}
public void setUser_Type(String user_Type) {
User_Type = user_Type;
}


public String viewUser(HttpServletRequest request, HttpServletResponse response)throws SQLException
{
String query="SELECT User_Name,Password FROM User";
Database cn= new Database();
cn.loadDriver();
cn.connect();

boolean chk =false;

String currentuser =(String) request.getSession().getAttribute("UserName");
String currentpassword = request.getParameter("Password");
String resultsetUser = null;
String resultsetPassword = null;
String redirectresponse = null;


ResultSet resultset = cn.result(query);
while(resultset.next()){
resultsetUser = resultset.getString("User_Name");
resultsetPassword = resultset.getString("Password");
resultset.close();
cn.disconnect();
if(currentuser.equals(resultsetUser) && currentpassword.equals(resultsetPassword)){

redirectresponse="/UserInput.jsp";

try {
response.sendRedirect("/UserInput.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

}else{
System.out.println("Invalid username and password");
redirectresponse="/error.jsp";

try {
response.sendRedirect("/error.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
return redirectresponse; } } }

After removing response.sendRedirect("/UserInput.jsp");from viewUser() method my Servlet's doget method is getting excuting again and again in continues loop and brower is showing Login window in waiting state and i have removed <jsp:include page="/Login.jsp" flush="true" ></jsp:include> from UserInput .

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright