aspose file tools*
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Question about url mapping and context" Watch "Question about url mapping and context" New topic
Author

Question about url mapping and context

HKVince Chan
Greenhorn

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.

<servlet>
<servlet-name>process</servlet-name>
<servlet-class>front.FormProcessor</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>process</servlet-name>
<url-pattern>/process</url-pattern>
</servlet-mapping>


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:
http://www.coderanch.com/t/83309/Tomcat/Tomcat-docpaths-relative-paths

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 :
/process
the browser assumes '/' means 'the root of the web server, which means:
http://www.foo.com/process

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
Greenhorn

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about url mapping and context