我需要帮助。代码显示错误。我认为我的 web.xml 文件有误。 因此,我创建了一个 servlet 系统,该系统使用 quartz CRON 调度程序 来创建作业。 在这种情况下,工作是在用户定义的号码之后唤醒用户。小时。代码非常简单,并且大部分没有错误。 servlet 映射 部分除外。在映射方面,我总是遇到一些困难,并且我已尽最大努力纠正我的错误,但似乎存在问题,因为它说“localhost 上的服务器 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>
最后是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)
最佳答案
如果您像使用 @WebServlet("/AlarmSystem")
那样通过注释定义 Web servlet您有一个已配置的 servlet 在上下文根中应答 /AlarmSystem
.
然后您在 web.xml
中定义了一个 servlet(您称为 frog 的那个)使用 <url-pattern>/AlarmSystem</url-pattern>
的上下文根 url 配置.因此,您实际上所做的是定义两个 servlet 映射,但它们都具有相同的上下文根。
为了解决这个问题,您应该在代码中通过注释或在 web.xml
中定义您的 servlet。但两者都不是。如果你保留你的 @WebServlet("/AlarmSystem")
定义,你可以删除所有<servlet>
和 <servlet-mapping>
您的 web.xml
中的标签.
关于tomcat - servlet 映射错误和/或 Tomcat 服务器本地主机未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24444817/