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?
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.
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.
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.
The deployment descriptor is a file named web. xml . It resides in the app's WAR under the WEB-INF/ directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With