简体   繁体   English

在Servlet中调用Java类

[英]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. 在我的servlet中,我调用了class.java(构造html表的类)的实例,以便在我的jsp中创建该表。

the servlet is like the following: 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);

in my jsp i can not display this table even empty or full. 在我的jsp中,我什至无法显示此表为空或满。

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. 问题是您没有对ConstruireReport()的返回值做任何事情,因此就迷路了。 You should set it as a request attribute so your JSP can find the string. 您应该将其设置为request属性,以便您的JSP可以找到字符串。

EDIT: Suggestion to use getWriter() on the servlet removed - misunderstood scenario. 编辑:建议在servlet上使用getWriter()删除-被误解的情况。

1) The instances of DeniedReportDisplay and LockedReportDisplay are created locally, no way to refer them once outside the if..else block. 1)DeniedReportDisplay和LockedReportDisplay的实例是在本地创建的,无法在if..else块之外一次引用它们。

2) The method invoked ( rd.ConstruireReport() ) returns a StringBuffer and you should store it somewhere. 2)被调用的方法(rd.ConstruireReport())返回StringBuffer,您应该将其存储在某个地方。 Try to use Response.getWriter() and put all the response string into this writer. 尝试使用Response.getWriter()并将所有响应字符串放入此编写器中。

3) Suggest you to find some good tutorial books about how to design Servlets/JSP, the solution you tried to build is quite wried. 3)建议您找到一些有关如何设计Servlets / JSP的优秀教程书,您尝试构建的解决方案实在令人担忧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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