This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help for Context root

 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

I am reading head first servlets & JSP, I am stuck in the very basic stage of web application development i.e. in the topic "mapping the logical name to a servlet class file"

I will summarize in short:

in HTML page we give "public URL name" of servlet in the <form method="POST" action="publicUrlName">, but browser prepends the context root on to the request & it looks like "POST/contextRoot/publicUrlName".

Here I am not understanding how browser is appending the context root, I read further lines in the book but I am not able to understand what exactly is context root? Why it is used? How browser knows what is context root that it appends in the request? I searched on internet but I didn't find simplified explanation

please help
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context root is the name of war file containg the servlet. You can treat context root as your web application name.
 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks shivendra

but still I want to know that how browser appends the context root? I mean how it knows what is context root (name) & why it needs to append that??

Thanks in advance
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a server you can have more than one application deployed, each having their own context path. Context path is kind of root directory where server will look for resource(for ex JSP). Suppose you have JSP with same name and different content in two application then context path is only way server will come to know which resource you want. Browser already knows the context root from your previous request(request which will send form to browser) so it just prepend it to get the other reource from the same application(context).
 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said "Browser already knows the context root from your previous request(request which will send form to browser)", but in request (i.e. form in the HTML code) we dont give context root, we give only public URL name of the servlet. So how browser can know about it?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Priti,

Its a simple logic used by Browser to check if URLs are bound to context(from the request URL) or not. If any action path or href has leading "/" , which means absolute , then Browser considers the URL itself has context root and do not prepend anything , else if there are no leading "/" , then browser assumes the url is part of the existing context and prepends it.

<!a href="/NewApp/test" />
<!a href="test" />

Note the difference above.
 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope my understanding is correct

--> The "action" path in HTML is related to the URL of the that HTML page. Accordingly browser appends the context name (web application name). This also depends on whether action path has leading "/".

Thanks Balu & Shivendra.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priti,
Let me explain it in detail.
1. I have a html page a.html where i have code written to call a servlet ("<form method="POST" action="publicUrlName">, "). And server will append the context when you submit this page from browser since it's not absolute.
2. Now first of all I have to acess the a.html and suppose it is in war file MyApp. So my caontext path will be MyApp
3. I will submit URL sth like Http://hostname:port/MyAppp/a.html
4. Browser will display a.html
5. When I will submit a.Html, browser will see the form action without "/"(so it's relative), it will take context from step 3 which is MyApp and prepend before action name. Now your URL will be Http://hostname:port/MyAppp/publicUrlName
6. In case your action is "/publicUrlName" your URL will become Http://hostname:port/publicUrlName

Hope this will be helpful.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priti Sharma wrote:I hope my understanding is correct

--> The "action" path in HTML is related to the URL of the that HTML page. Accordingly browser appends the context name (web application name). This also depends on whether action path has leading "/".


Thats perfectly right.
 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivendra tripathi wrote:Priti,
Let me explain it in detail.
1. I have a html page a.html where i have code written to call a servlet ("<form method="POST" action="publicUrlName">, "). And server will append the context when you submit this page from browser since it's not absolute.
2. Now first of all I have to acess the a.html and suppose it is in war file MyApp. So my caontext path will be MyApp
3. I will submit URL sth like Http://hostname:port/MyAppp/a.html
4. Browser will display a.html
5. When I will submit a.Html, browser will see the form action without "/"(so it's relative), it will take context from step 3 which is MyApp and prepend before action name. Now your URL will be Http://hostname:port/MyAppp/publicUrlName
6. In case your action is "/publicUrlName" your URL will become Http://hostname:port/publicUrlName

Hope this will be helpful.



Thanks Shivendra for such an elaborative explanation.
 
Priti Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:
Priti Sharma wrote:I hope my understanding is correct

--> The "action" path in HTML is related to the URL of the that HTML page. Accordingly browser appends the context name (web application name). This also depends on whether action path has leading "/".


Thats perfectly right.


Thank you Balu for confirming my understanding
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic