How can i redirect to my jsp page from servlet in init() method.

Discussions

Web tier: servlets, JSP, Web frameworks: How can i redirect to my jsp page from servlet in init() method.

  1. How can i redirect to my jsp page from servlet in init() method..Becoz that servlet is calling while server startsup..so im writing some piece of code in init() method..after that i want to redirect to some jsp page ...
    using RequestDispatcher..its not possible..becoz
    RequestDispatcher rd = sc.getRequestDispatcher("goto.jsp");
    rd.foward(req,res);
    Here the request and response are null objects..
    So mi question can frame as how can i get request/response in servlet's init method()..
    thanx in advance..
  2. You cannot do that from the init() method.

    A client request (ie from a browser) is coming to your servlet's service method. This service method will call doGet(req,res) or doPost(req,res) or others depending on the request method (GET,POST,HEAD,etc..)

    So, you can only redirect when a request has been made (ie your servlet is called from the browser). To redirect from doGet or doPost, use the method you described.

    Rgrds,
    Klaas