2010-06-01 95 views
0

我調用了一個class.java的實例(一個構造html表的類),以便在我的jsp中創建這個表。在我的servlet中調用一個servlet中的java類

該servlet是這樣的:

String report=request.getParameter("selrep"); 
String datev=request.getParameter("datepicker"); 
String op=request.getParameter("operator"); 
String batch =request.getParameter("selbatch"); 

System.out.println("report kind was:"+report); 
System.out.println("date was:"+datev); 
System.out.println("operator:"+op); 
System.out.println("batch:"+batch); 


if(report.equalsIgnoreCase("Report Denied")) 
{ 
    DeniedReportDisplay rd = new DeniedReportDisplay(); 
    rd.ConstruireReport(); 
} 
else if(report.equalsIgnoreCase("Report Locked")) 
{ 
    LockedReportDisplay rl = new LockedReportDisplay(); 
    rl.ConstruireReport(); 
} 

request.getRequestDispatcher("EspaceValidation.jsp").forward(request, response); 

在我的jsp我無法顯示此表甚至是空或滿。

注:爲例該構造否認報告一類具有這樣的結構:

/*constructeur*/ 
        public DeniedReportDisplay() {} 

/*Methodes*/ 

@SuppressWarnings("unchecked") 

       public StringBuffer ConstruireReport() 

       { 
        StringBuffer retour=new StringBuffer(); 
        int i = 0; 
        retour.append("<table border = 1 width=900 id=sheet align=left>");  
        retour.append("<tr bgcolor=#0099FF>"); 
        retour.append("<label> Denied Report</label>"); 
        retour.append("</tr>");      
        retour.append("<tr>"); 

String[] nomCols ={"Nom","Prenom","trackingDate","activity","projectcode","WAName","taskCode","timeSpent","PercentTaskComplete","Comment"}; 
//String HQL_QUERY = null;      
    for(i=0;i< nomCols.length;i++) 
    { 
    retour.append(("<td bgcolor=#0066CC>")+ nomCols[i] + "</td>"); 

    } 
    retour.append("</tr>"); 

    retour.append("<tr>"); 

       try { 

s= HibernateUtil.currentSession(); 
tx=s.beginTransaction(); 
Query query = s.createQuery("select opcemployees.Nom,opcemployees.Prenom,dailytimesheet.TrackingDate,dailytimesheet.Activity," + 
    "dailytimesheet.ProjectCode,dailytimesheet.WAName,dailytimesheet.TaskCode," + 
    "dailytimesheet.TimeSpent,dailytimesheet.PercentTaskComplete from Opcemployees opcemployees,Dailytimesheet dailytimesheet " + 
    "where opcemployees.Matricule=dailytimesheet.Matricule and dailytimesheet.Etat=3 " + 
    "group by opcemployees.Nom,opcemployees.Prenom"); 


    for(Iterator it=query.iterate();it.hasNext();) 
     {                   
         if(it.hasNext()){ 

         Object[] row = (Object[]) it.next(); 


         retour.append("<td>" +row [0]+ "</td>");//Nom 
         retour.append("<td>" + row [1] + "</td>");//Prenom 
         retour.append("<td>" + row [2] + "</td>");//trackingdate 
         retour.append("<td>" + row [3]+ "</td>");//activity 
         retour.append("<td>" + row [4] +"</td>");//projectcode 
         retour.append("<td>" + row [5]+ "</td>");//waname 
         retour.append("<td>" + row [6] + "</td>");//taskcode 
         retour.append("<td>" + row [7] + "</td>");//timespent 
         retour.append("<td>" + row [8] + "</td>");//perecnttaskcomplete 
         retour.append("<td><input type=text /></td>");//case de commentaire 
            } 
            retour.append("</tr>"); 


     } 
//terminer la table. 
          retour.append ("</table>"); 

          tx.commit(); 


       } catch (HibernateException e) 
       { 
    retour.append ("</table><H1>ERREUR:</H1>" +e.getMessage()); 
       e.printStackTrace(); 
       } 

       return retour; 
} 

感謝您的幫助。

+3

您遺漏了提問的部分。你有什麼問題?你得到了什麼結果 - 任何錯誤信息或類似信息? 'Construire' + 1 + – BlairHippo 2010-06-01 14:28:54

+2

+ - 同時構建和查詢,現在效率很高!我想從現在開始我會用法語編碼我的東西。 – mdma 2010-06-01 14:29:56

+0

有人可以請縮進源代碼嗎? – 2010-06-01 14:31:10

回答

1

1)DeniedReportDisplay和LockedReportDisplay的實例是在本地創建的,無法在if..else塊之外引用它們。

2)調用的方法(rd.ConstruireReport())返回一個StringBuffer,你應該把它存儲在某個地方。嘗試使用Response.getWriter()並將所有響應字符串放入此writer。 3)建議你找一些關於如何設計Servlets/JSP的優秀教程書籍,你試圖構建的解決方案是相當困難的。

+0

將其寫入響應將導致'forward()'上的'IllegalStateException'。 – BalusC 2010-06-01 16:21:57

1

問題是你沒有對ConstruireReport()的返回值做任何事情,所以它只是丟失了。您應該將其設置爲請求屬性,以便您的JSP可以找到該字符串。

編輯:建議使用servlet上的getWriter()刪除 - 誤解場景。

+1

我很抱歉地說,但是這個答案令人驚訝*。現在他不能轉發到期望的JSP而不面對'IllegalStateException'。 – BalusC 2010-06-01 14:45:00

+0

我以爲這是從* JSP中調用*我沒有做太多的JSP開發,所以如果我錯過了一些基本的東西,請告訴我! – mdma 2010-06-01 14:47:32

+0

不,請閱讀他的問題的第一句和第一個代碼段的最後一行。 – BalusC 2010-06-01 14:49:49