13

I was reading Head First JSP and Servlets book. I was going through the mapping of servlet. And my doubt here is

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>com.avinash.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/ServletBeer.do</url-pattern>
</servlet-mapping>
  1. Why should the url-pattern start with forward slash(/)?
  2. What does the forward slash(/) represent? Does it represent our webapp name?
  3. What happens if the url-pattern do not start with forward slash(/)?
  4. Is it a specification to start with forward slash(/)?

And in the book it is clearly mentioned:

Don't forget the forward slash(/) in the url-pattern.

Can you explain?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44

2 Answers2

7

The / in the url-pattern means the root of the web application that uses this web.xml. So when you give /ServletBeer.do. That means http://<yourhost>:<port>/<your context Root>/ServletBeer.do. When ever you hit this url, the servlet you which have mapped with this i.e com.avinash.TestServlet will be invoked.

Its not mandatory that you have to use / in all your url mapping - that purely depends on how you want to configure it. / is just a regular expression. You can also use *.jsp in your url-pattern - which will actually invoke this servlet everytime you request any jsp in your web- application. Consider the section 12.2 Specification of Mappings ** from **JSR-000315 Java Servlet 3.0 Final Release

12.2 Specification of Mappings In the Web application deployment descriptor, the following syntax is used to define mappings:

■ A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

■ A string beginning with a ‘*.’ prefix is used as an extension mapping.

■ The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).

■ A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

■ All other strings are used for exact matches only

shikjohari
  • 2,278
  • 11
  • 23
  • The / in the url-pattern means the root of the web application that uses this web.xml. One small doubt .You mean to say if my webapp is deployed in the folder Beer-app. So are you referring Beer-app as the root of the web application – Avinash Reddy Feb 04 '15 at 08:51
  • lets say your Beer-app is the folder deployed under tomcat/webapps. then Beer-app will be your root of the app. Please accept if the above answer is what you were looking for – shikjohari Feb 04 '15 at 09:33
  • As you said http:///ServletBeer.do. When i hit the url as http://localhost:8083/ServletBeer.do i get HTTP Status 404 - /ServletBeer.do. My application is in beerV1 folder, Should the url be http://localhost:8083/beerV1/ServletBeer.do. And when i access the url as follows http://localhost:8083/beerV1/ServletBeer.do i get the output. I mean to say the expected servlet is hit and i can see the output – Avinash Reddy Feb 25 '15 at 17:10
  • yep, updated I missed the context root. In your case beerV1 is to context root – shikjohari Feb 26 '15 at 05:12
  • The official reference is already given. Great! Have to thump up for @shikjohari – Rui Feb 10 '17 at 23:17
0

On the above questions i could find answer for one of the questions.

What happens if the url-pattern do not start with forward slash(/). I have checked it but i don't remember

When i started tomcat by removing the / in the url-pattern i got the following error

Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> ServletBeer.do in servlet mapping
    at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3267)
    at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3242)
    at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1324)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1348)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:880)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:378)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5343)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

This indicates that if we are not using extension mapping compulsory the url-pattern should start with /

Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44
  • The other question which i figured out was 2. What does the forward slash(/) represent. Does it represent our webapp name. The slash(/) represents the context root of the web app. – Avinash Reddy Feb 26 '15 at 16:38