aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Please help for Context root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Please help for Context root" Watch "Please help for Context root" New topic
Author

Please help for Context root

Priti Sharma
Greenhorn

Joined: Oct 15, 2008
Posts: 15
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

Joined: Aug 26, 2008
Posts: 263
Context root is the name of war file containg the servlet. You can treat context root as your web application name.


SCJP 1.5(97%) My Blog
Priti Sharma
Greenhorn

Joined: Oct 15, 2008
Posts: 15
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

Joined: Aug 26, 2008
Posts: 263
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

Joined: Oct 15, 2008
Posts: 15
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

Joined: Jan 01, 2009
Posts: 874


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

Joined: Oct 15, 2008
Posts: 15
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

Joined: Aug 26, 2008
Posts: 263
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

Joined: Jan 01, 2009
Posts: 874

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

Joined: Oct 15, 2008
Posts: 15
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

Joined: Oct 15, 2008
Posts: 15
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
 
subject: Please help for Context root