• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leave it off and it won't work.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic