wood burning stoves*
The moose likes Tomcat and the fly likes Question about url mapping and context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Question about url mapping and context" Watch "Question about url mapping and context" New topic

Question about url mapping and context

HKVince Chan

Joined: Feb 25, 2004
Posts: 4
It is a question about the url-mapping and the application context.

Using tomcat 5.1 windows version (run without IIS), run on port 8080.

I have a application called "myapp" and I create a context file for it with the following line inside.

<Context path="/myapp" debug="0" reloadable="true" crossContext="true" useNaming="true">

And I need a servlet called "FormProcessor" which is processing form submit.
It is under package "front". So, I create the mapping inside the web.xml under myapp/WEB-INF.



However, if I want to submit the form to this this servlet, I need to mention the action as "myapp/process" rather than "/process". Why the form cannot distinguish the right context itself ?

Thanks for your attention and graceful help.

[ June 21, 2004: Message edited by: HKVince Chan ]
[ June 21, 2004: Message edited by: HKVince Chan ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Because to a browser, the context simply looks like an HTTP path.

This is a somewhat related discussion regarding image urls:

Remember the 'form' is just a dumb thing. The browser will submit the following:

For a page served from http://www.foo.com/myapp/form.jsp
and your action tag is :
the browser assumes '/' means 'the root of the web server, which means:

The browser doesn't know that /myapp is a signifigant part of the URL, to a browser it looks like a directory. (to the *browser*, which is the one doing the form post).

So you need to either
1) always prepend the result of request.getContextPath() (Which is what I do these days)
2) use absolute URLS

You *might* be able to get away with <form action="process" ... > But I wouldn't count on that. What happens when you decide to shift a directory around?
[ June 24, 2004: Message edited by: Mike Curwen ]
HKVince Chan

Joined: Feb 25, 2004
Posts: 4
Thanks a lot, Mike. I think I just make a silly mistake and misunderstanding on the context path. Your answer is quite useful for me.
Again, thank you.
I decide to use the request.getContextPath() which is fine for my case.
[ June 26, 2004: Message edited by: HKVince Chan ]
Don't get me started about those stupid light bulbs.
subject: Question about url mapping and context
Similar Threads
Difficulties with setting up Apache tomcat: 'Need a little help.
Servlet not showing up in Browser
The requested resource (/mycode/AA) is not available
problem with deployment
About servlet-mapping