Set servlet as default home page in web.xml

Shravan J Kumar picture Shravan J Kumar · Aug 15, 2015 · Viewed 17.1k times · Source

I've a servlet registered in web.xml as below.

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern>/RequestManager</url-pattern>
</servlet-mapping>

Basically I want to call this servlet as my default home page when I open http://localhost:8080/appname. So, I tried registering it as welcome file in same web.xml as below:

<welcome-file-list>
    <welcome-file>Manager</welcome-file>
</welcome-file-list>

But, when I run the project, I get an error saying "requested resource not available". However, if I write in the url with my servlet URL pattern, it works fine.

Answer

BalusC picture BalusC · Aug 15, 2015

Specify an empty string as servlet's URL pattern.

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

See also:


Unrelated to the concrete problem, the <welcome-file> should represent an URL path, not a servlet name. It'd have worked if you specifed <welcome-file>RequestManager</welcome-file>. But this affects all subfolders. Actually, the <welcome-file> has an entirely different meaning than "home page file" you've had in mind. It represents the default resource which should be served when a folder is been requested.