简体   繁体   中英

How to hit a servlet class with ServletContextListener from Jsp page button click

I have a servlet implementing ServletContextListener.Now as per my need i have to send some parameters from jsp page submit button click to this servlet but it is not hitting the servlet file on submit button click..

Here is my Servlet code..

@WebListener()
public class MyContext implements ServletContextListener {

Timer timer = new Timer();

@Override
public void contextInitialized(ServletContextEvent event) {

    Calendar date = Calendar.getInstance();
    date.set(

            Calendar.DAY_OF_MONTH, 12);
    date.set(Calendar.HOUR, 00);
    date.set(Calendar.MINUTE, 11);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);


    timer.schedule(
            new MyTask(),
            date.getTime(),
            1000 * 60 * 60 * 24 * 7);

}

@Override
public void contextDestroyed(ServletContextEvent event) {
    timer.cancel();
}

}

And here is my JSP page..

<form action="/MyContext/" method="GET">
        <label>Set Date: </label>
        <input type="text" name="date" id="date">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label>Set Time: </label>
        <input type="text" name="time" id="time">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="Submit" name="Submit" value="Submit" id="Submit">
    </form>

This is my web.xml file ..

<servlet>
    <servlet-name>MyContext</servlet-name>
    <servlet-class>MyContext</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyContext</servlet-name>
    <url-pattern>/MyContext</url-pattern>
</servlet-mapping>
<listner>
    <listner-class>
 MyContext
    </listner-class>>
</listner>>

Please guys help me .. Thanks in advance...

The class MyContext implementing ServletContextListener is not a Servlet that process your requests. Listeners wait for some events to happen and gives us a handlers method to handle the event. In the case of ServletContextListener we can handle the initialization and destruction of the ServletContext. Refer this post to get a better understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM