2014-02-24 43 views
0

我試圖從數據庫中獲取數據並將其顯示在另一個JSP上,但有一個錯誤java.lang.IllegalStateException:在提交響應之後無法轉發,位於com.java.QTD.QuestionOfTheDay.doGet()

"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

'字符串URL = NULL;'' DB DBConnector =新DBConnector(URL);'是這樣行嗎?你將Null傳遞給'DBConnector'? – Babel

回答

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); 
} 

錯誤指出當某些內容發送到響應時您無法轉發。

2

消息說明了這一切。

基本上,當您的web應用程序執行某些操作時,響應是「提交」,導致它將開始寫入客戶端。發生這種情況時,將寫入響應標頭。但是,如果請求需要重定向,則不能寫入標頭......'因爲您重定向到的servlet很可能需要在響應中輸出不同的標頭。

在這種情況下,當您撥打response.getWriter()時,響應會被提交。當您稍後致電rd.forward(request, response)時,已爲時過晚,您將獲得例外。

你需要重新思考你的doGet()方法的邏輯...

0

RequestDispatcher.forward()之前被調用,響應不應該被提交。

在你的情況下,它最有可能是servlet導致響應在forward()之前被提交。

這裏有響應的原因得到承諾:

Causes of Response getting committed.

相關問題