This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

What does (/) mean when I map my Servlets to URLs?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I will post screenshot of a book I am currently reading (Head First Servlets & JSP). What I am curious about is what exactly means slash (/) when I map Servlet to an URL? What does it refer to when I make URL request?
As of example here: /Public1 and /Public2

Thanks
image.png
page 48 in Head First's 2nd edition
page 48 in Head First's 2nd edition
 
Saloon Keeper
Posts: 12893
280
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The starting slash refers to the context root.

The context root is the base URL of your application. Almost each application container that you use (Tomcat, GlassFish, WildFly, etc.) has its own way of determining the context root of the applications that they host.

For instance, a common way to do it in Tomcat is to name your WAR a certain way. If you deploy a file named my#example#application##2.4.0.war to a Tomcat instance listening at localhost:8080, you can reach your application at http://localhost:8080/my/example/application/. This is the context root.

If you configured a servlet mapping with a path /Public1, you can access the servlet by sending requests to http://localhost:8080/my/example/application/Public1.
 
Stefan Jankovic
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mate. Starting to get a picture. When you say base URL, what do you mean? Is it protocol + server name + path? Notice how I omitted resource at the end. Is that correct?

And if I mapped to Public1 and not /Public1, what would that mean then?

Hope I am not asking something stupid.. Thanks in advance!

 
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you'd like a pretty complete breakdown of the parts of a URL, I did one here: https://coderanch.com/t/735561/application-servers/Tomcat-Greenhorn

As an editorial note, I'll also add that "backslash" isn't good form in a URL and that people who pronounce "slash" as "backslash" irritate me. The slash character, also known as "stroke",  "virgule" and probably other things, is a long and honored character. The backslash, as far as I know, was invented somewhere around World War II, and I'm not sure what the benefit was, but old Teletype™ terminals often had both characters on the same key, where backslash was entered via the SHIFT key. Anyway, just a digression. I'm glad you  didn't make that mistake!

One other thing to note is that URLs are NOT filename paths. Yes, parts of a URL look like filename paths, but they are actually simply identification sequences. The web application can use the internal part of a URL (the part following host locator and application context path) any way it wants. For example, using web.xm to map a URL pattern to route the request to a servlet.

The confusion there is that by convention, if the web framework cannot resolve a URL pattern in any other name, it uses that pattern to locate and return a webapp resource.

The term "resource" is important. If you unzip ("explode") a WAR file, the resulting files and directories are resources, and they are always addressed as though they were their own little filesystem rooted in the top-level "directory" of the WAR. But the reason why you should call them "resources" rather than "files" is because the canonical form of a WAR is a single ZIP (JAR) file. In that case, you cannot directly access the WAR resources via standard filesystem functions like open/close/read/write.

In fact, you really shouldn't attempt to read "files" that are inside a WAR, because if the WAR has not been exploded, the attempt to open the "file" will fail. You can use the HttpServletRequest getResource() and getResourceAsStream() methods, however, and you should always address resources by their absolute resource path. That is, if your WAR has a directory named "js" and you need the "ajax.js" file, its resource path would be /js/ajax.js.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic