aspose file tools*
The moose likes Servlets and the fly likes servlet mapping clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet mapping clarification" Watch "servlet mapping clarification" New topic
Author

servlet mapping clarification

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Im trying to figure out how this mapping relates to the url on the browser.



if i go to http://127.0.0.1:8080/ with the above mapping it will hit my index servlet and display my page.




if i go to http://127.0.0.1:8080/ with the above mapping it will not got to my servlet. However if i go to http://127.0.0.1:8080/command/index I will hit the servlet and the page will display
How come one works and the other doesnt?

I guess maybe im not grasping the entire mapping concept correctly. In the past I would map all my servlets individually begining with the root /

like so:


So how would I then If I were to go to www.mysite.com
Have the url display as www.mysite.com/command/index and my home page display automatically?

Or am i looking at this the wrong way?


Here is my current Servlet mapping
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

John Schretz wrote:
How come one works and the other doesnt?

Per section 10.10 of the Servlet Specification:
Servlet 3.0 Specification wrote:
If a Web container receives a valid partial request, the Web container must examine the welcome file list defined in the deployment descriptor. The welcome file list is an ordered list of partial URLs with no trailing or leading /. The Web server must append each welcome file in the order specified in the deployment descriptor to the partial request and check whether a static resource in the WAR is mapped to that request URI. If no match is found, the Web server MUST again append each welcome file in the order specified in the deployment descriptor to the partial request and check if a servlet is mapped to that request URI. The Web container must send the request to the first resource in the WAR that matches. The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request. (Emphasis added)


The reason for this is that the welcome file list applies to ALL paths within a web app. It is the default file to load when going to a "directory". So a leading slash does not make sense. By default, the web server will look for index.html. So if I navigate to example.com/stuff, the web server will look for the page /stuff/index.html (that is example.com/stuf/index.html). If I navigate to example.com/more/things, the web server will look for the page /more/things/index.html. I can use the welcome-file-list to override, or added to, the list of files I want the web server to look for when a partial URL is provided.

John Schretz wrote:
So how would I then If I were to go to www.mysite.com
Have the url display as www.mysite.com/command/index and my home page display automatically?


Map a redirect Servlet (or JSP) to the root context. Have that redirect servlet redirect the client to /command/index. Be sure you understand Forward versus redirect.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

John Schretz wrote:Im trying to figure out how this mapping relates to the url on the browser.
I guess maybe im not grasping the entire mapping concept correctly. In the past I would map all my servlets individually begining with the root /


Sorry... I forgot to comment on this. A servlet mapping is different than the welcome list. As I mentioned in my first reply, since the welcome list applies to all partial URL's (i.e. "directories") and is appended to the partial URL, a leading slash does not make sense. However, with servlet mapping, they are absolute mappings from the root of the web application and therefore require a leading slash. That's why there is a difference. See section 12.2 of the servlet specification for information on the what patterns are allowed in a URL mapping for a servlet.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188

Ok I think this is what you were getting at. Let me know if this is correct. Also I am using Bears frontman in my project.

I changed the welcome file back to:



Then I created my entry servlet:



Then I created my servlet that forwards to the jsp page.



Below is my full web.xml

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I was not aware that you were using Front Man (or any other framework). I am not familiar with Front Man beyond knowing it is a Web App front Controller. As such, my previous comments may not apply since Front Man may provide functionality to do what you are attempting to accomplish.

Also, can you indicate what your goal is. My initial understanding of your question was that you wanted it so that if someone navigated to a single resource, namely www.mysite.com and only www.mysite.com, that they would be redirected (not forwarded) to www.mysite.com/command/index. The suggestion I made would only work for people navigating to www.mysite.com. If they navigated to www.mysite.com/page1.html they would not be redirected to www.mysite.com/command/index nor to www.mysite.com/command/index/page1.html. Also, I suggested a redirect, not a forward (see below). However, it now looks like you may be attempting to put access control in place so that if an a non authenticated user (i.e. some one that is not logged in) navigates to any resource in your web application, they are redirected to a log in page. Or are you trying to make it so that any request is sent through a Controller?


Redirect vs Forward
As I mentioned in my initial post, it is important to understand the difference between a forward and a redirect. Let's say I have a very basic page called somePage.html that is nested in the directory docs/info. In other words, you navigate to www.mysite.com/docs/info/somePage.html to view that page. That page contains a relative link to anotherPage.html such that the anchor tag contains the attribute href="anotherPage.html". Therefore, if I navigate to www.mysite.com/docs/info/somePage.html and click on the link, I go to www.mysite.com/docs/info/anotherPage.html.

If I have a redirect configured to handle requests to "/" and redirect those requests to /docs/info/somePage.html, when I go to www.mysite.com the web server sends a redirect back to the client (i.e. the browser) to the page /docs/info/somePage.html. The browser URL shown in the browser address bar changes from www.mysite.com to www.mysite.com/docs/info/somePage.html and the browser submits a new request to the web server for www.mysite.com/docs/info/somePage.html. As a result, when the browser displayes the page, the relative link on the page resolves to www.mysite.com/docs/info/anotherPage.html.

By contrast, if instead I have a forward configured to handle requests to "/", when a user goes to www.mysite.com, the web server internally gets the content from /docs/info/somePage.html and returns it to the client. The client is unaware of this. As such, the URL in the address bar does not change. So the URL still reads www.mysite.com. Moreover, the relative link on the page resolves to www.mysite.com/anotherPage.html. This is most likely an incorrect resolution of the URL. There are times (mostly in the case of servlet mappings rather than html pages) that this may be desirable.

So the use case for a forward and a redirect are different. They each have there uses depending on the goal.


Can you please clarify what your goal is. I will likely need to bow out of the discussion since I am not familiar with Front Man and I do not want to give you incorrect information on how to accomplish your goal if Front Man potentially provides functionality to accomplish your goal. Someone else familiar with the framework can jump in to help. But I think they too will need some clarification of your goal in order to better assist you. (Also, this thread may need to be moved by a bartender to the Other Application Frameworks forum if that is the case).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

The FrontMan controller is a servlet like any other and so needs no special considerations for mapping.

But I'm also unsure exactly what the issue is.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet mapping clarification
 
Similar Threads
Frontman 2.0 vs /WEB-INF
JSTL & Servlet-mappings in web.xml
FrontMan Commands ?
Frontman 2 FileNotFoundException ?
Using a filter with frontman