Reputation: 31
I am trying to fetch data from database and it display it on another JSP
, but there is an error
"java.lang.IllegalStateException: Cannot forward after response has been committed"
please anybody give me solution
code is
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
System.out.println("Control at Question Of The day ************************************* ");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
String username= (String) session.getAttribute("username");
System.out.println("Username from session == == = == == = ="+username);
//Code for creation Dynamic number....
xyz uniquecode=new xyz();
String uni=uniquecode.UniqueCode();
System.out.println("dynamic number creation== ==== ==== == "+uni);
if(username!=null)
{
System.out.println("Session is not nulll block ... ");
String url = null;
DBConnector db=new DBConnector(url);
Connection con=db.getConnection();
System.out.println("Connection establish successfully ... ... .. .. .. ");
String query="select Question, choiceA,choiceB,choiceC,choiceD from question_master where Question_id=?";
try{
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, "92");
java.sql.ResultSet rs= ps.executeQuery();
List<QTD> questions = new ArrayList<QTD>();
if(rs.next())
{
QTD question = new QTD();
question.setQuestion(rs.getString(1));
System.out.println("Question ==== = "+rs.getString(1));
question.setOptA(rs.getString(2));
System.out.println("Answer A ==== = "+rs.getString(2));
question.setOptB(rs.getString(3));
System.out.println("Answer B ==== = "+rs.getString(3));
question.setOptC(rs.getString(4));
System.out.println("Answer C ==== = "+rs.getString(4));
question.setOptD(rs.getString(5));
System.out.println("Answer D ==== = "+rs.getString(5));
questions.add(question);
// System.out.println("************************************ List Data ****************************");
//System.out.println("************************************ List Data ****************************" +question) ;
RequestDispatcher rd=request.getRequestDispatcher("/html/QTD.jsp");
rd.forward(request, response);
}
else{
System.out.println("there is not data ");
}
//System.out.println("List from Database ========= "+questions);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
}
else{
System.out.println(" ********************* inside username is null block ************************** ");
RequestDispatcher rd=request.getRequestDispatcher("html/login.jsp");
out.print("Sorry! Wrong Some Error is Occure, Please try again");
rd.forward(request, response);
}
}
Upvotes: 0
Views: 4064
Reputation: 15446
Before RequestDispatcher.forward()
is called, the response should not be committed.
In your case its most probably that the servlet is causing the response to be committed before forward().
Here are the reasons for response getting committed:
Causes of Response getting committed.
Upvotes: 0
Reputation: 719249
The message says it all.
Basically, the response is "committed" when your webapp does something that causes it to start being written to the client. When that happens, the response headers are written. But if the request needs to be redirected, you can't have the headers written ... 'cos the servlet you are redirecting to would most likely need to output different headers in the response.
In this case, the response gets committed when you call response.getWriter()
. When you later call to rd.forward(request, response)
, it is too late, and you will get the exception.
You need to rethink the logic of your doGet() method ...
Upvotes: 2
Reputation: 57421
Add the code in the beginning of you method
HttpSession session=request.getSession();
String username= (String) session.getAttribute("username");
if (username==null) {
RequestDispatcher rd=request.getRequestDispatcher("html/login.jsp");
out.print("Sorry! Wrong Some Error is Occure, Please try again");
rd.forward(request, response);
}
The error states that you can't forward when something was sent to the response.
Upvotes: 2