This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes DD: url-pattern and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "DD: url-pattern and "/" - explanation please ..." Watch "DD: url-pattern and "/" - explanation please ..." New topic
Author

DD: url-pattern and "/" - explanation please ...

Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Hi everyone,

I'm just starting with servlest and reading Head First - Servlets and JSP.
There is a sentence, which says that when mapping URLs to servlets,
<url-pattern> must begin with "/", eg:

<url-pattern>/something</url-pattern>

My question is: why do I have to begin with "/", what would happen
if I didn't start with it, like:

<url-pattern>something</url-pattern> ???


Many thanks,

Adrian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Leave it off and it won't work.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Earth will stop rotating on its axis.
The seas will dry up.
The forests will burn to a crisp and, more importantly, you won't be able to hit your servlets.

There is a link to the servlet spec in my signature.
In it, you will find a complete explanation of servlet mappings.
It's a good read.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
I have written a note in HFS myself regarding your question. If the "/" is missing, it would mean relative to something. But what that something is when a path is specified in web.xml? It is quite different from request.getRequestDispatcher("someurl.jsp") where the call is associated with a request object and hence a URL where "someurl.jsp" is relative to.
Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Thanks for the replays guys !

Hmmm ... I asked myself this question after posting on the forum
but to be honest I'm still not sure :/

<servlet>
<servlet-name>My internal servlet name</servlet-name>
<servlet-class>com.nowhere.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>My internal servlet name</servlet-name>
<url-pattern>Servlet.do</url-pattenr>
</servlet-mapping>

So why can't container figure it out ?

Thanks for you patience - it's not only servlets that are new
to me but all web technology (I don't even know HTML but working
on it )

Cheers,

Adrian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Originally posted by Adrian Sosialuk:
So why can't container figure it out ?
That's like asking "So I typed fro instead of for, why can't the the complier figure it out?"

The containers follow the rules of the Servlet Specification. There's no "figuring it out".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

P.S. If your next question is "why is the Spec like that", Alec hit the nail on the head with the difference between page-relative and server-relative addressing.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Trying to write servlets and learn web technology without even knowing HTML or what it is used for doesn't make a whole lot of sense. You will find yourself running in circles because you don't know what anything talks about. You can intelligently look at any of the specifications.

Do you have ANY programming experience? If you have some C or C++ you might be able to scrape by with the java stuff as long as you understand the main concepts behind OOP.

One thing I've learned, also being new to the technology, is that specifications are your friend and don't fight them because you can't do anything about them! Just follow them and your path will be much easier.


[ December 07, 2007: Message edited by: Bryce Martin ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Adrian Sosialuk:

So why can't container figure it out ?


I'm actually thankful that containers don't try to 'figure things out for you.

As we learned with HTML over the last decade, the result of interpreters that allow sloppiness is more sloppiness. Eventually, the sloppiness gets so bad and the level of sloppiness tolerance between different interpreters became so disparate that it became almost impossible anything that will work in all environments.

With HTML, the move towards the stricter XHMTL has helped out a lot.
Luckily, most servlet containers stick to the specification and steer clear of things like this. The result is that it is fairly easy to write web apps that can be deployed in any spec compliant container.
 
 
subject: DD: url-pattern and "/" - explanation please ...
 
Similar Threads
Cannot find file..? Help
url-pattern for Exact Match
need help: .jsp to .jspf mapping
Servlet filter not getting invoked
servlet mapping problem