Results 1 to 3 of 3
  1. #1
    samiahmed is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default i need help for ServletContext

    hello everybody Actually i'm trying to type a ServletContext but i Got the following Error

    MyListener.java:25: contextDestroyed(javax.servlet.ServletContextEvent ) in MyLis
    tener cannot implement contextDestroyed(javax.servlet.ServletContextEvent ) in ja
    vax.servlet.ServletContextListener; overridden method does not throw java.lang.E
    xception
    public void contextDestroyed(ServletContextEvent sce)throws Exception
    ^
    MyListener.java:13: contextInitialized(javax.servlet.ServletContextEve nt) in MyL
    istener cannot implement contextInitialized(javax.servlet.ServletContextEve nt) i
    n javax.servlet.ServletContextListener; overridden method does not throw java.la
    ng.Exception
    public void contextInitialized(ServletContextEvent sce) throws Exception

    ^
    2 errors


    and my code is

    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.ServletContext.*;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;

    public final class MyListener implements ServletContextListener
    {
    Connection con;
    public void contextInitialized(ServletContextEvent sce) throws Exception
    {

    ServletContext ctx=sce.getServletContext();
    String dburl=ctx.getInitParameter("dburl");
    String uname=ctx.getInitParameter("uname");
    String pwd=ctx.getInitParameter("pwd");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("dburl,uname,pwd") ;
    ctx.setAttribute("Connection",con);

    }
    public void contextDestroyed(ServletContextEvent sce)throws Exception
    {

    ServletContext ctx=sce.getServletContext();
    con=(Connection)ctx.getAttribute("Connection");
    con.close();
    }
    }
    :confused:

  2. #2
    sagarbirari is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    hello

    remove throws clause from both method signature
    public void contextInitialized(ServletContextEvent sce) throws Exception
    public void contextDestroyed(ServletContextEvent sce)throws Exception

    regards

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    And, if anything you call in those methods can throw an exception, you must catch those exceptions within those methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •