Welcome File List in Servlet

When the application load and request URL map to root of the applicaion then the container invokes the welcome page without giving the name in URL.
We can set welcome filelist by using <welcome-filelist> tag in the deployement descriptor.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>FirstServlet</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>FirstServlet</display-name> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> </web-app>
Servlet Welcome Filelist Example
FirstServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); ServletConfig config=getServletConfig(); String name=config.getInitParameter("adminName"); String password=config.getInitParameter("password"); out.println("Welcome Servlet initilization Parameters"); out.println("<BR>"); out.println("User Name is "+name); out.println("<BR>"); out.println("Password is "+password); out.println("<BR>"); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5"> <display-name>WelcomeFilelistDemo</display-name> <welcome-file-list> <welcome-file>FirstServlet</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>FirstServlet</display-name> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> <init-param> <param-name>adminName</param-name> <param-value>Sunil</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>Kumar</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app>
Output
welcome file list
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE
 



 

Latest Tutorial


Most Visited Tutorial