I need help.The code shows error.I think my web.xml file is wrong. So I created a servlet system that uses a quartz CRON scheduler to create a job. In this case, the job is to wake the user up after a user-defined no. of hours.The code is pretty straightforward and for the most part error free. Except for the servlet mapping part. I have always faced some difficulty when it comes to mapping and I have tried my level best to rectify my errors but it seems there is a problem as it says that the "Server Tomcat v7.0 Server at localhost failed to start."
Here is my code- 1. The HTML file
<!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(basically the job execution)
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. The Servlet (AlarmSystem.java file)
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.The Web.XML page
<?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>
And finally here is the 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)
If you by annotations define a web servlet as you have done with @WebServlet("/AlarmSystem")
you have a configured servlet answering at the context root /AlarmSystem
.
Then you are defining a servlet (the one you call frog) in your web.xml
with the context root url configuration of <url-pattern>/AlarmSystem</url-pattern>
. So what you in effect have done is to define two servlets mappings, but they both have the same context root.
In order to fix this you should define your servlet in your code by annotations or in the web.xml
but not in both. If you keep your @WebServlet("/AlarmSystem")
definition, you can remove all <servlet>
and <servlet-mapping>
tags in your web.xml
.