Set servlet as default home page in web.xml [duplicate]
Asked Answered
B

3

6

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.

Brenneman answered 15/8, 2015 at 0:51 Comment(0)
H
4

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.

Holiness answered 15/8, 2015 at 14:36 Comment(2)
this does not work for me .... currently i am working with servlet version 3.1. It will be great if you provide solution thanks. @HolinessQuatre
It works for me and anyone else in a blank project. It's more likely that you've something else in your project which is colliding with it.Holiness
R
1

You can use index.jsp to forward to your servlet.

<jsp:forward page="servlet_context">

and add index.jsp as welcome file in web.xml

Rowen answered 15/8, 2015 at 2:5 Comment(0)
M
1

inside servlet class you can forward Control Using :

request.getRequestDispatcher("forward page URL").forward(req,res);

or else if you are using JSP then use

<% RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.forward(request, response); %>

or

<jsp:forward page="relative URL" />
Mitchel answered 15/8, 2015 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.