我正在尝试从数据库中获取数据并将其显示在另一个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);
}
}
答案 0 :(得分:2)
在方法的开头添加代码
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);
}
错误表明,当某些内容发送到响应时,您无法转发。
答案 1 :(得分:2)
信息说明了一切。
基本上,当您的webapp执行导致启动写入客户端的操作时,响应将“已提交”。发生这种情况时,会写入响应头。但是,如果需要重定向请求,则不能编写头文件......因为您重定向到的servlet很可能需要在响应中输出不同的标头。
在这种情况下,当您致电response.getWriter()
时,会提交回复。当您稍后致电rd.forward(request, response)
时,为时已晚,您将获得例外。
您需要重新考虑doGet()方法的逻辑......
答案 2 :(得分:0)
在调用RequestDispatcher.forward()
之前,不应提交响应。
在你的情况下,最有可能的是servlet导致响应在forward()之前被提交。
以下是提交回复的原因: