Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure welcome file list in web.xml

I have this in my web.xml document. I am trying to have a welcome list so I dont need to type the path for the home page anymore. But everytime a clicked the application in my tomcat page it displays The requested resource is not available.

<listener>     <listener-class>web.Init</listener-class> </listener>  <welcome-file-list>     <welcome-file>index.jsp</welcome-file> </welcome-file-list>  <servlet>     <servlet-name>index</servlet-name>     <servlet-class>web.IndexServlet</servlet-class> </servlet>  <servlet-mapping>     <servlet-name>index</servlet-name>     <url-pattern>/index</url-pattern> </servlet-mapping> 

My servlet for the jsp page

package web;  import java.io.IOException;  import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  import org.apache.log4j.Logger;  public class IndexServlet extends HttpServlet {     private Logger logger = Logger.getLogger(this.getClass());     private RequestDispatcher jsp;      public void init(ServletConfig config) throws ServletException     {         ServletContext context = config.getServletContext();         jsp = context.getRequestDispatcher("/WEB-INF/jsp/index.jsp");     }      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException     {         logger.debug("doGet()");         jsp.forward(req, resp);      } } 

Why is that it is still not working?I still need to type the /index in my url...How to do this correctly?

like image 288
PeterJohn Avatar asked Feb 01 '13 14:02

PeterJohn


People also ask

What is the use of welcome file list in web xml?

The web. xml file's welcome-file-list property is used to establish a list of welcome files. If you don't supply a file name while loading the project in the browser, the tag <welcome-file-list> is used to define the files that will be called by the server by default.

What is the use of welcome file list?

The welcome files mechanism allows you to specify a list of files that the web container will use for appending to a request for a URL (called a valid partial request) that is not mapped to a web component.

What does web xml contain?

web. xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method.

In which folder we can put web xml?

The deployment descriptor is a file named web. xml . It resides in the app's WAR under the WEB-INF/ directory.


1 Answers

You need to put the JSP file in /index.jsp instead of in /WEB-INF/jsp/index.jsp. This way the whole servlet is superflous by the way.

WebContent  |-- META-INF  |-- WEB-INF  |    `-- web.xml  `-- index.jsp 

If you're absolutely positive that you need to invoke a servlet this strange way, then you should map it on an URL pattern of /index.jsp instead of /index. You only need to change it to get the request dispatcher from request instead of from config and get rid of the whole init() method.

In case you actually intend to have a "home page servlet" (and thus not a welcome file — which has an entirely different purpose; namely the default file which sould be served when a folder is being requested, which is thus not specifically the root folder), then you should be mapping the servlet on the empty string URL pattern.

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

See also Difference between / and /* in servlet mapping url pattern.

like image 129
BalusC Avatar answered Sep 18 '22 19:09

BalusC