How to redirect welcome page in Java Web Project using Jsp and Servlet

Redirect a welcome page or set any page as the welcome page for a Java project. In this tutorial, we will focus on the web.xml in the java web application. Probably you will get the answer to the following questions.

  •  What is a web.xml file and user of web.xml?
  • How to set a welcome page of a Java Web Application.

If you are creating any web application project. Dynamic Java web Project or Maven Project, There will be one common file called web.xml

What is a web.xml

web.xml is the configuration file For web applications in Java. the communication with the servlet containers (tomcat). which classes to load, what parameters to set in the context, and how to handle requests coming from browsers.

It contains URL mapping, Servlets, Filters, error pages, security constraints, welcome files, listeners.

How to set a welcome page of a Java Web Application.

To set the welcome page we going to use URL mapping with XML <welcome-file> tag. So we have tag <welcome-file>  that will contain under the <welcome-file-list> file.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
  <welcome-file>WelcomeCtl</welcome-file>
  </welcome-file-list>
</web-app>

In the above code “WelcomeCtl” is a name of the servlet name as WelcomeCtl. When we will run the project and the browser will send a request this Servlet URL will be called in the getting method by default. which will redirect the request to related JSP file (welcome.jsp)