Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Charles Lyon's book mock exam 4

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this mock exam, question 7:

Assuming the container's security settings allow access to foreign applications, how could a servlet (servletA) in application A (deployed to /appA) invoke a servlet (servletB) in application B (/appB) which resides in the same Web container?
A. The servlet in A can invoke the URL for the servlet in B.


According to the book ,
A is feasible, but won't communicate directly with the servlet-indeed, communication would leave and then re-enter the container to gain access to the resource this way. This method could be used to include the results of a foreign URL target, including resources residing outside the container.

But as I tried, I cannot forward or include another servlet in another application.

I have a servlet A which can be accessed at http://localhost:8080/ch12email/
I have another serlvet B in the same container, which can be accessed at http://localhost:8080/ch02email
In servlet B, my code is:

The container looks for a path relative to /ch02email/ch12email , but it cannot find it. The container returns an error.


How can I implement what option A describe?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default it won't work in Tomcat (assuming you are using Tomcat). You need to use a Tomcat setting in the server.xml or config.xml:
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Frits. Thanks for your suggestion. I added <Context crossContext="true"/> as the first element in Tomcat's server.xml.
I deploy the servlet which is accessed by the context root /ch12email.
I run the servlet which is accessed by the context root /ch02email.
But I get this:

type Status report
message /ch02email/ch12email
description The requested resource is not available.


It is because the container tries to look for a servlet under /ch02email, but under ch02email, there is not a servlet being accessed by /ch02email/ch12email.
The code of the servlet being access by /ch02email
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
Interface ServletContext
getRequestDispatcher
...
The pathname must begin with a / and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts.
...
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I learn from chapter 6 of Charles Lyon's book, if I want to forward to a foreign context from /ch02email to /ch12email :

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic