0

I have a welcome page called index2.html and I want to initialize this page when user sends a get request.

I have created an IndexServlet to catch get requests. Then I try to send redirect to my index page but infinite loop starts as expected. How can I initialize welcome page from servlet?

@WebServlet(name = "IndexServlet", urlPatterns = {"/"})
public class IndexServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //add some information
    response.sendRedirect("index2.html");
  }

}

web.xml file

<welcome-file-list>
    <welcome-file>
        index2.html
    </welcome-file>        
</welcome-file-list>
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • Do you have any other mappings in your web.xml file ? Can you post your complete web.xml ? – Vasu Mar 18 '17 at 08:58
  • 1
    Mapping a servlet to "/" makes it receive **all** the requests to the webapp. Why do you have this servlet in the first place? Your html page is a static page, and it's in the welcome files. There is nothing to "initialize". – JB Nizet Mar 18 '17 at 08:58
  • @javaguy I don't have anything in my web.xml file except session-timeout property. Actually I don't have any other page. – hellzone Mar 18 '17 at 09:02
  • @JBNizet My application doesn't have a login page or something like that and at welcome page I will show some data coming from my database. – hellzone Mar 18 '17 at 09:06
  • Then your html page shouldn't be a static page, but a dynamic one (i.e. a JSP or example), and you shouldn't redirect, but you should forward. – JB Nizet Mar 18 '17 at 10:48

0 Answers0