如何调用servlet类文件,该文件使用Quartz Scheduler在浏览器上显示jasper报告

时间:2014-12-18 12:47:19

标签: java servlets quartz-scheduler

用于在目录中存储jasper报表的简单Java应用程序的Quartz Scheduler正在工作,因为我使用public void execute而不是public static void main并在作业类中执行,如下所示,

我不打算以正确的方式传递servlet id的URL,当我运行调度程序servlet时,它会触发作业类文件,这将调用servlet类。

帮帮我     import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

 public class Starter_jasper implements Job {

 public Starter_jasper()
{
    JobExecutionContext jec = null;
    try {
        execute(jec);
    } catch (JobExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void execute(JobExecutionContext jec) throws JobExecutionException{
//include(Jasper_database.class);
System.out.println("" + new Date());

Jasper_database jb = new Jasper_database();
jb.execute();
//fill your jobs here
}}   this code works for a simple java 

它适用于一个简单的java应用程序,但我的问题是如何为servlet类做到这一点, 我直接运行我的servlet类,我没有使用任何JSP,我的调度程序和触发器在另一个servlet中 如下图所示

      import java.io.IOException;

   import javax.servlet.GenericServlet;
   import javax.servlet.ServletConfig;
   import javax.servlet.ServletException;
   import javax.servlet.ServletRequest;
   import javax.servlet.ServletResponse;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import job.quartz.gateway.GatewayPlugin;

   import org.quartz.JobBuilder;
   import org.quartz.JobDetail;
   import org.quartz.ScheduleBuilder;
   import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
      import org.quartz.SimpleScheduleBuilder;
      import org.quartz.Trigger;
   import org.quartz.TriggerBuilder;
   import org.quartz.impl.StdSchedulerFactory;

   import com.jasper.servlet.JasperSReport;

  /**
    * Servlet implementation class QuartzServlet
   */
     public class QuartzServlet extends GenericServlet {
   private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public QuartzServlet() {
    super();
    // TODO Auto-generated constructor stub
}

public void init(ServletConfig config)throws ServletException{

    super.init(config);
    Scheduler sched;
    try{
        sched = StdSchedulerFactory.getDefaultScheduler();
        sched.start();
  //    JobDetail jd = new JobDetail("myjob", sched.DEFAULT_GROUP,GatewayPlugin.class);
        JobDetail job = JobBuilder.newJob(GatewayPlugin.class).
                        withIdentity("JasperSReport").build();
        ScheduleBuilder ScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().
                                          withIntervalInSeconds(20).
                                          repeatForever();
        Trigger trigger = TriggerBuilder.newTrigger().
                          withIdentity("QuartzTrigger").
                          withSchedule(ScheduleBuilder).
                          startNow().
                          build();

        sched.scheduleJob(job, trigger);

    }catch(SchedulerException e){
        System.out.println("Scheduler exception at class file shcedule or trigger"+e);

    }
}

public void service(
        ServletRequest arg0, ServletResponse arg1)throws ServletException, IOException{

  }
}

1 个答案:

答案 0 :(得分:0)

您无法实例化Servlet,因为它是容器问题,但您可以:

request.getRequestDispatcher("/TARGET_SERVLET").forward(request,response);