Solved

servlet context syntax

Posted on 2006-11-08
15
234 Views
Last Modified: 2013-11-24
i have this .....

TreeNode rootNode = (TreeNode)getServletContext().getAttribute("mediaLibRootTreeNode"); which resides in a back end section

now i have to recreate this in the front end section

now normally i have this ..
getServlet().getServletContext().getAttribute("rolms_front_dir").toString() to get to the front bit.
but now how do i set my rootNode of TreeNode type to get the servlet attribute?
i'm not sure if i'm explaining this properly but i'm hoping you guys will understand this when u see it.
0
Comment
Question by:blue-genie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 39

Author Comment

by:blue-genie
ID: 17896655
ok let me try explain it this way. i have a getContext.setAttribute("") that gets set on startup of tomcat.
i have 2 folders, the admin and front bits, and in the front, i have a jsp that needs to get hold of that attribute.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17896675
use something like:

Object value = application.getAttribute(name);
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17896695
objects, it returns a null, but the value is set when the server starts up - it's in the logs.
i'm thinking it doesn't know where the servletcontext is. so i have to set somethign to the rolms_front_dir bit, but then get another attribute.

i want to do something like this

Object value = pageContext.getServletConfig().getServletContext().getAttribute("rolms_front_dir").toString();
            out.println(value); //this prints out correctly
            TreeNode rootNode=(TreeNode)value.getAttribute("mediaLibRootTreeNode");

the other bit gives me a cannot resolve symbol

symbol  : method getAttribute (java.lang.String)
location: class java.lang.Object
            TreeNode rootNode=(TreeNode)value.getAttribute("mediaLibRootTreeNode");
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 17897049
where exactly is that code?

>    TreeNode rootNode=(TreeNode)value.getAttribute("mediaLibRootTreeNode");

'value' is a string, it doesn't have a getAttribute method

do u mean:

TreeNode rootNode=(TreeNode) pageContext.getServletConfig().getServletContext().getAttribute("mediaLibRootTreeNode");
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17897266
that code is in the jsp in the front bit

thing is the servlet is in the back, and the attribute gets set there, and i'm trying to access it from the front.
The web application has a administration section and a front view.
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17898501
ok, part 2, we've managed to pull something back but...when we getClass().getName() returns TreeNode
but if we cast to TreeNode - throws a classcast exeptinon?
0
 
LVL 92

Expert Comment

by:objects
ID: 17902235
can u post your code
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17904577
thanks objects.... appreciate it

my serlet on start up calls

treeNodeCreator.buildCourseListWithLearningProgramesAsTree(getServletContext());

the buildCourse... method does a whole bunch of things then at the end

servletContext.setAttribute("mediaLibRootTreeNode", rootNode);

in my jsp....

we've been fiddling around with this a bit, so might look a bit disjointed...

TreeNode rootNode = null;

try{
      TreeNode rootNode =getServletContext().getContext("/" + getServletContext().getAttribute("rolms_admin_dir")).getAttribute("mediaLibRootTreeNode");
} catch (ClassCastException ce) {
      out.println(ce.getMessage());
}
out.println("Object type: " + rootNode.getClass().toString());//returns TreeNode

i need what's returned by the servet attribute to be cast to the TreeNode so that i can access it's methods....

shout if this is still not enough.

            
            
0
 
LVL 92

Expert Comment

by:objects
ID: 17904600
there is no casting in that code??
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17904832
yeah sorry
when we try TreeNode rootNode = (TreeNode)getServletContext().getContext("/" + getServletContext().getAttribute("rolms_admin_dir")).getAttribute("mediaLibRootTreeNode");

it throws the error.
0
 
LVL 92

Expert Comment

by:objects
ID: 17904861
> out.println("Object type: " + rootNode.getClass().toString());//returns TreeNode

that doesn't make sense, if u were getting a CCE then rootNode would be null and that line would not return TreNode
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17904879
ok, like is said, we've been playing around with the code and putting out prints and trying all sorts of wierd stuff.

bottom line, when we return the attribute from the servlet attribute and do a getClass on it, it brings back TreeNode, however when we explicitly cast it to TreeNode it throws the exception.
0
 
LVL 92

Expert Comment

by:objects
ID: 17904912
if thats the case then they must be different TreeNode classes.
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17904918
is that possible if they're both the same package and all?
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17963232
managed to come right, thanks for your efforts anyway.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 65
Need help aligning JRadioButton that put in a box using GridBagLayout 2 42
Setup GlassFish 4 55
junit example issue 2 56
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question