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
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.
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.
Loudly announcing something is true and finding out you're wrong makes you feel foolish.
Finding out you're wrong and refusing to admit it makes you LOOK foolish.
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