Change default homepage in root path to servlet with doGet

How can I change the contents of this page to something else ?

Open the underlying JSP/HTML/XHTML file in a text editor. This page is identified by <welcome-file> entry in web.xml. If it’s e.g. <welcome-file>index.jsp</welcome-file>, then you need to open /index.jsp file in your project’s web content in the IDE builtin text editor.


Or, at the very least (if the former is impossible): Can I use a permanent redirect on root path to avoid the user from seeing this page?

This question is badly thought out. You don’t want to redirect the visitor forth and back all time. You want to map your servlet on webapp root. In order to map a servlet on root path, use the empty string URL pattern "" instead of the default servlet URL pattern "/" as in your attempt.

@WebServlet("")

Or if you’re still not on Servlet 3.0 yet, here’s the old fashioned web.xml way.

<servlet-mapping>
    <servlet-name>yourHomeServlet</servlet-name>
    <url-pattern></url-pattern> <!-- Yes, empty string! -->
</servlet-mapping>

If you still keep using the default servlet URL pattern of "/", then you have to take over all responsibilities of the container’s builtin default servlet such as serving up static resources like CSS files, adding browser-caching headers, supporting file download resumes, etc. See also the first related link below for detail.

At least there’s no need to abuse <welcome-file> for this. This does not represent the “homepage file” as many starters seem to expect. This represents “folder’s default file to serve when any subfolder is requested”. Thus not only on /, but also on /foo/, /bar/, etc.

See also:

Leave a Comment