gpt4 book ai didi

java.lang.IllegalStateException : Cannot forward after response has been committed, 位于 com.java.QTD.QuestionOfTheDay.doGet()

转载 作者:行者123 更新时间:2023-12-01 18:36:00 25 4
gpt4 key购买 nike

我试图从数据库获取数据并将其显示在另一个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);
}

该错误指出,当某些内容发送到响应时,您无法转发。

关于java.lang.IllegalStateException : Cannot forward after response has been committed, 位于 com.java.QTD.QuestionOfTheDay.doGet(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21981285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com