简体   繁体   中英

calling a java class in a servlet

in my servlet i called an instance of a class.java( a class that construct an html table) in order to create this table in my jsp.

the servlet is like the following:

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

in my jsp i can not display this table even empty or full.

note: exemple a class that construct denied Report has this structure:

   /*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;
 }

thanks for help.

The problem is that you are not doing anything with the return value from ConstruireReport(), so it just get's lost. You should set it as a request attribute so your JSP can find the string.

EDIT: Suggestion to use getWriter() on the servlet removed - misunderstood scenario.

1) The instances of DeniedReportDisplay and LockedReportDisplay are created locally, no way to refer them once outside the if..else block.

2) The method invoked ( rd.ConstruireReport() ) returns a StringBuffer and you should store it somewhere. Try to use Response.getWriter() and put all the response string into this writer.

3) Suggest you to find some good tutorial books about how to design Servlets/JSP, the solution you tried to build is quite wried.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM