0

ヘルプが必要です。コードにエラーが表示されます。web.xml ファイルが間違っていると思います。そこで、Quartz CRON スケジューラを使用してジョブを作成するサーブレット システムを作成しました。この場合、ジョブは、ユーザー定義の no の後にユーザーを起こすことです。コードは非常に簡単で、ほとんどの部分でエラーがありません。サーブレットのマッピング部分を除く。マッピングに関しては常にいくつかの問題に直面しており、自分のレベルでエラーを修正するために最善を尽くしましたが、「ローカルホストのサーバー Tomcat v7.0 サーバーが起動に失敗しました」と表示されているため、問題があるようです。

これが私のコードです-1.HTML ファイル

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter after how many hours you'd like to be woken up-</title>
</head>
<body>
<form name="form" action="AlarmSystem" method="post">
Hours  :<input type="text" name="hr"><br>
Minutes:<input type="text" name="min"><br>
Seconds:<input type="text" name="sec"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

2. OldJob(基本的にジョブ実行)

package mayur;

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

public class OldJob implements Job 
{
    public void execute(JobExecutionContext context)
        throws JobExecutionException 
        {
          System.out.println("Wake-up!!");
        }
}

3. サーブレット (AlarmSystem.java ファイル)

package mayur;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mayur.OldJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Servlet implementation class NewTrigger
 */
@WebServlet("/AlarmSystem")
public class AlarmSystem extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public AlarmSystem() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String sec=request.getParameter("sec");
        String min=request.getParameter("min");
        String hr=request.getParameter("hr");
        PassingMethod(sec,min,hr);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    public void PassingMethod(String sec,String hr, String min)
    {
            //connect to the real job execution
            JobDetail job=JobBuilder.newJob(OldJob.class).withIdentity("FirstJob","group").build();
            //create trigger
            Trigger trigger= TriggerBuilder.newTrigger().withIdentity("FirstJob","group").withSchedule
            (CronScheduleBuilder.cronSchedule("0/"+sec+" 0/"+min+" 0/"+hr+" * * ?")).build();
            //schedule it
            Scheduler scheduler;
            try {
                scheduler = new StdSchedulerFactory().getScheduler();
                 scheduler.start();
                   scheduler.scheduleJob(job, trigger);
            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
}

4.Web.XML ページ

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>frog</display-name>
  <servlet>
        <servlet-name>frog</servlet-name>
        <servlet-class>mayur.AlarmSystem</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>frog</servlet-name>
        <url-pattern>/AlarmSystem</url-pattern>
    </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

そして最後に、ここにSTACKTRACEがあります

Caused by: java.lang.IllegalArgumentException: The servlets named [frog] and [mayur.AlarmSystem] are both mapped to the url-pattern [/AlarmSystem] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:335)
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/frogg]]
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
4

1 に答える 1

0

アノテーションを使用して Web サーブレットを定義する@WebServlet("/AlarmSystem") と、コンテキスト ルートで応答するサーブレットが構成されます/AlarmSystem

web.xml次に、 のコンテキスト ルート URL 構成を使用して、サーブレット (カエルと呼ぶもの) を定義しています<url-pattern>/AlarmSystem</url-pattern>。したがって、実際に行ったことは、2 つのサーブレット マッピングを定義することですが、どちらも同じコンテキスト ルートを持っています。

これを修正するには、注釈によって、またはweb.xml両方ではなく、コードでサーブレットを定義する必要があります。@WebServlet("/AlarmSystem")定義<servlet>を保持する場合<servlet-mapping>は、web.xml.

于 2014-06-27T10:43:36.677 に答える