This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext related

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a valid deployment descriptor (WEB.XML) containing the following code

<context-param>
<param-name>bestwebsite</param-name>
<param-value>www.examulator.com</param-value>
</context-param>
<servlet>
<servlet-name>GetInitParameter</servlet-name>
<servlet-class>com.examulator.GetInitParameter</servlet-class>
<init-param>
<param-name>bestwebsite</param-name>
<param-value>www.javaranch.com</param-value>
</init-param>
</servlet>

And given that cfg is a valid ServletConfig object and ctx is a valid ServletContext object which of the following statements are true?

Choose at least one answer.
a. cfg.getInitParameter("bestwebsite") will return www.examulator.com Incorrect
b. ctx.getInitParameter("bestwebsite") will return www.javaranch.com Incorrect
c. ctx.getInitParameter("bestwebsite") will return www.examulator.com Correct
d. cfg.getServletContext().getInitParameter("bestwebsite"); will cause a compile time error Incorrect
e. ctx.getServletConfig().getInitParameter("bestwebsite"); will cause a compile time error

explaination

You need to know the difference between ServletConfig and ServletContext for the purpose of the exam. ServletContext might well have been called ApplicationContext to make its purpose clearer, but it wasn't. You can get a context from a config, but you cannot directly get a config from a context.

-My question is will "e" be the right answer or not?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is will "e" be the right answer or not?

There's a getServletContext() in ServletConfig. But do you see any getServletConfig() method in ServletContext ?
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But my question is, it will give compile error or not?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my question is, it will give compile error or not?

Do you remember what you've learned in SCJP ? What happens when you call a method which does not exist ?
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wont compile. Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic