Apache Tomcat 4.1.x: how to get front page without redirect like slashdot.org

A

Alex Martinoff

Hello all,

Suppose I'm running slashdot.org except coded with Java instead of
Perl. I want the main page served by a servlet named index but without
any redirects. So when the user types

http://slashdot.org/

into their browser and goes there they won't be redirected to
http://slashdot.org/index. The real slashdot.org doesn't automatically
redirect you to index.pl. You just see http://slashdot.org/ in your
location bar. So, I tried to set this up with Tomcat and have only
found a partial-solution that isn't completely satisfactory.

My application's web.xml contains

<!-- ... -->
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>article</servlet-name>
<url-pattern>/article</url-pattern>
</servlet-mapping>
<!-- ... -->

Then, I edited the site-wide web.xml in conf/ so that the default
servlet is no longer mapped to "/" but instead to the various types my
site uses like:
<!-- ... -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<!-- ... -->

If I don't do that then my images and css will no longer load. I
thought just simply making the change I made to my application's
web.xml would be sufficient, but apparently the meaning of
servlet-mapping to "/" isn't the intuitive thing of just mapping
requests for "/" to index and nothing else; it maps all other request
URIs not given servlet-mappings in those 2 web.xml files to the index
servlet instead of to the default servlet. That's what servlet-mapping
to something broader like "/*" or "/**/*" or something should do;
mapping to just "/" shouldn't give the effect it does. So if I do
http://localhost:8080/foo then I'll get the index servlet unless
foo=article or blah.gif, etc. I'd rather get a NOT FOUND message for
random URIs.

I suppose editing org.apache.catalina.servlets.DefaultServlet is a
possibilty, but there HAS to be a better approach! For example, can I
set up a default servlet that checks if the request is for "/" and if
it is then pass the request and response to the doGet (or doPost, as
the case may be) index serlet method? (If it's not for "/" then pass
the request and response to the usual DefaultServlet method.)

This is frustrating! Any and all replies really appreciated. Thanks a
bunch!
 
K

Kees de Kooter

(e-mail address removed) (Alex Martinoff) wrote in
Hello all,

Suppose I'm running slashdot.org except coded with Java instead of
Perl. I want the main page served by a servlet named index but without
any redirects. So when the user types

http://slashdot.org/

into their browser and goes there they won't be redirected to
http://slashdot.org/index. The real slashdot.org doesn't automatically
redirect you to index.pl. You just see http://slashdot.org/ in your
location bar. So, I tried to set this up with Tomcat and have only
found a partial-solution that isn't completely satisfactory.

My application's web.xml contains

<!-- ... -->
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>article</servlet-name>
<url-pattern>/article</url-pattern>
</servlet-mapping>
<!-- ... -->

Then, I edited the site-wide web.xml in conf/ so that the default
servlet is no longer mapped to "/" but instead to the various types my
site uses like:
<!-- ... -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<!-- ... -->

If I don't do that then my images and css will no longer load. I
thought just simply making the change I made to my application's
web.xml would be sufficient, but apparently the meaning of
servlet-mapping to "/" isn't the intuitive thing of just mapping
requests for "/" to index and nothing else; it maps all other request
URIs not given servlet-mappings in those 2 web.xml files to the index
servlet instead of to the default servlet. That's what servlet-mapping
to something broader like "/*" or "/**/*" or something should do;
mapping to just "/" shouldn't give the effect it does. So if I do
http://localhost:8080/foo then I'll get the index servlet unless
foo=article or blah.gif, etc. I'd rather get a NOT FOUND message for
random URIs.

The trouble with servlet mappings is that the url pattern you can use is
not very fine grained. I hope in the future you can use regular
expressions for it.

For now would suggest introducing jsp pages. In the web.xml you then add
an entry like

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

All requests to yourdomain.org/ are handled by this index.jsp

You can drop all the servlet mappings, and your css and gif mappings
(which looks like a workaround to me) can also go.

Let me know if you need more info!
Kees de Kooter
 
M

Michal Dzirba

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

AFAIK Since Servlet API 2.3 a servlet can be a welcome-file
The problem is that itll probably show at the and of the url, which is
not what was needed. [The same thing happens with jsp's. ]


Michal.
 
A

Alex Martinoff

The trouble with servlet mappings is that the url pattern you can use is
not very fine grained. I hope in the future you can use regular
expressions for it.

For now would suggest introducing jsp pages. In the web.xml you then add
an entry like

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

All requests to yourdomain.org/ are handled by this index.jsp

But there is a redirect. Requests to mydomain.org/ are redirected to
mydomain.org/index.jsp. Also, there's no need for the welcome-file to
be a jsp. The welcome file can be a relative servlet url such as just
plain old index for my index servlet. The requests to mydomain.org/
are redirected to mydomain.org/index.
 
A

Alex Martinoff

The trouble with servlet mappings is that the url pattern you can use is
not very fine grained. I hope in the future you can use regular
expressions for it.

For now would suggest introducing jsp pages. In the web.xml you then add
an entry like

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

All requests to yourdomain.org/ are handled by this index.jsp

But there is a redirect. Requests to mydomain.org/ are redirected to
mydomain.org/index.jsp. Also, there's no need for the welcome-file to
be a jsp. The welcome file can be a relative servlet url such as just
plain old index for my index servlet. The requests to mydomain.org/
are redirected to mydomain.org/index.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top