5

I want to starting by fireing the servlet class before loading a jsp page, because i need to populate some data from the database in a jsp page. Servlet mapping in web.xml

    <servlet>
        <servlet-name>Index</servlet-name>
        <servlet-class>com.Teklabz.Servlets.IndexServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Index</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>

but it didn't work, when tracing the code it's never reach the servlet class. Also i was trying to use ServletContextListener like this link, but I faced the same problem.

listener code:

public class ServletListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

web.xml code:

    <listener>
        <listener-class>com.techlabz.listener.ServletListener</listener-class>
    </listener>

I don't know what am doing wrong.

Community
  • 1
  • 1
IBRA
  • 1,502
  • 3
  • 24
  • 56
  • Your question is confusing. Please be clear what is not working? And what do you want to achieve. Happy to help you. – Ramesh PVK Jan 08 '13 at 10:46
  • What do mean by starting the servlet ? Container can load the servlet on start up means what ever inside `init` method will be executed at startup – amicngh Jan 08 '13 at 10:47
  • 1
    It sounds to me like you want to actually navigate or submit to the servlet and then -forward- to the JSP. That way the servlet can do the database stuff, prepare a nice object structure in the request scope and then the JSP can take this information and display it. – Gimby Jan 08 '13 at 10:47
  • @RameshPVK thanks, the code never reach the servlet code, always start with jsp page. i tried to use servlet listener but didn't work ether. – IBRA Jan 08 '13 at 10:48
  • @Gimby exactly, i'm trying to retrieve data from db in servlet and send it to jsp page. but the servlet didn't run when the application startup – IBRA Jan 08 '13 at 10:51
  • in this case you need to call the servlet then forward the request to jsp rather than direct accessing jsp , other way around is put you db code in init method of com.Teklabz.Servlets.IndexServlet – amicngh Jan 08 '13 at 10:56

2 Answers2

3

There are number of ways you can achieve this ..

  1. Either you can populate the data in service method com.Teklabz.Servlets.IndexServlet and then set the data in request attribute and then forward to that jsp.
  2. If you want to use loadonstartiup then you can populate the data from db in com.Teklabz.Servlets.IndexServlet servlet's init method and then set it in some accessible scope(request,session,context) and by direct accessing jsp get the data from that scope.
  3. In listener also you can do this but in that case also you need to set the data in some scope.
amicngh
  • 7,831
  • 3
  • 35
  • 54
0

your code is absolutely right.you need to Add annotation @WebListener

  @WebListener
   public class ServletListener implements ServletContextListener{
    //your code
   }
Nitesh Singh Rajput
  • 607
  • 1
  • 7
  • 24