我正在尝试从数据库中获取数据,它显示在另一个JSP
上,但是有一个错误
"java.lang.IllegalStateException: Cannot forward after response has been committed"
请有人给我解决方案
代码是
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);
}
}
方法的开头添加代码
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);
}
该错误指出,当某些内容发送到响应时,您无法转发。
消息说明了一切。
基本上,当您的 Web 应用程序执行导致它开始写入客户端的操作时,响应是"提交"的。 发生这种情况时,将写入响应标头。 但是,如果需要重定向请求,则无法写入标头...'因为您要重定向到的 servlet 很可能需要在响应中输出不同的标头。
在这种情况下,当您调用 response.getWriter()
时,将提交响应。 当你稍后打电话给rd.forward(request, response)
时,为时已晚,你会得到例外。
您需要重新考虑 doGet() 方法的逻辑...
在调用RequestDispatcher.forward()
之前,不应提交响应。
在您的情况下,很可能是 servlet 导致在 forward() 之前提交响应。
以下是响应提交的原因:
响应的原因。