0

我需要帮助。代码显示错误。我认为我的 web.xml 文件有误。所以我创建了一个 servlet 系统,它使用石英 CRON 调度程序来创建作业。在这种情况下,工作是在用户定义的号码后唤醒用户。小时。代码非常简单,并且大部分没有错误。除了servlet 映射部分。在映射方面,我总是遇到一些困难,我已经尽力纠正我的错误,但似乎有问题,因为它说“本地主机上的服务器 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. Servlet(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>

最后是堆栈跟踪

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 servlet,就像您已经完成的那样,@WebServlet("/AlarmSystem") 那么您在上下文根处有一个配置的 servlet 应答/AlarmSystem

然后,您将在您web.xml的上下文根 url 配置中定义一个 servlet(您称之为青蛙) <url-pattern>/AlarmSystem</url-pattern>。因此,您实际上所做的是定义两个 servlet 映射,但它们都具有相同的上下文根。

为了解决这个问题,您应该在代码中通过注释或在代码中定义您的 servlet,web.xml但不能同时在两者中定义。如果保留定义,则@WebServlet("/AlarmSystem")可以删除.<servlet><servlet-mapping>web.xml

于 2014-06-27T10:43:36.677 回答