views:

70

answers:

3

Hi,

I have a web app, I want to define my index.jsp file to be shown when the entered url is like:

www.mysite.com
www.mysite.com/
www.mysite.com/index.jsp

but if any other url is entered, like:

wwww.mysite.com/g

I want a particular servlet to handle the request. In my web.xml file, I am doing this:

<servlet>
    <servlet-name>ServletCore</servlet-name>
    <servlet-class>com.me.test.ServletCore</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletCore</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

so that is letting the ServletCore servlet pick up any url, but as expected it is taking over even the:

www.mysite.com/index.jsp

type urls. How can I define it in such a way to work how I want?

Thank you

A: 

The problem is that the servlet will take the entire root namespace. To stop it doint that, move it into a subfolder, then you can get what you want, e.g.

<servlet-mapping>
    <servlet-name>ServletCore</servlet-name>
    <url-pattern>/core/*</url-pattern>
</servlet-mapping>

You can then unify the two namespaces (the root and the core/ subdir) using apache rewrite rules. Or using a redirect filter, that dispatches the request to the appropriate url using a mapping table. This would map

  index.jsp -> index.jsp
  everything else -> core/$1

So that requests to index.jsp go there, and all other requests go do the servlet.

mdma
+1  A: 

The url-pattern of /* listens on every request URL. Rather put an <error-page> entry in web.xml which listens on HTTP status code 404 (Not Found).

<error-page>
    <error-code>404</error-code>
    <location>/error</location>
</error-page>

And then obviously map the servlet on an url-pattern of /error or whatever you like as long as the <location> matches this.

BalusC
A: 

Just make sure your webapp run as ROOT by naming your app directory as webapps/ROOT or use ROOT.war.

Then add this to your web.xml,

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
ZZ Coder