Reputation: 589
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?
Upvotes: 41
Views: 252846
Reputation: 51
I simply declared as below in web.xml file and Its working for me :
<welcome-file-list>
<welcome-file>/WEB-INF/jsps/index.jsp</welcome-file>
</welcome-file-list>
And NO html/jsp pages present in public directory except static resources(css, js, images). Now I can access my index page with URL like : http://localhost:8080/app/ Its calling /WEB-INF/jsps/index.jsp page. When hosted live in production the final URL looks like https://eisdigital.com/
Upvotes: 0
Reputation: 18975
This is my way to setup Servlet as welcome page.
I share for whom concern.
web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Demo</servlet-name>
<servlet-class>servlet.Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
Servlet class
@WebServlet(name = "/demo")
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
}
}
Upvotes: 1
Reputation: 526
I saw a nice solution in this stackoverflow link that may help the readers of the defulat servlet handling issue by using the empty string URL pattern "" :
@WebServlet("")
or
<servlet-mapping>
<servlet-name>yourHomeServlet</servlet-name>
<url-pattern></url-pattern> <!-- Yes, empty string! -->
</servlet-mapping>
Upvotes: 4
Reputation: 1108692
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.
Upvotes: 64
Reputation: 1
Its based on from which file you are trying to access those files.
If it is in the same folder where your working project file is, then you can use just the file name. no need of path.
If it is in the another folder which is under the same parent folder of your working project file then you can use location like in the following /javascript/sample.js
In your example if you are trying to access your js file from your html file you can use the following location
../javascript/sample.js
the prefix../ will go to the parent folder of the file(Folder upward journey)
Upvotes: -2
Reputation: 35008
I guess what you want is your index servlet to act as the welcome page, so change to:
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
So that the index servlet will be used. Note, you'll need a servlet spec 2.4 container to be able to do this.
Note also, @BalusC gets my vote, for your index servlet on its own is superfluous.
Upvotes: 24