Hi

I'm new to JSP and am having a right battle with session scope. Here's what I'm doing:

I have created a class with some variables and methods. As I want to be able to access the contents of my created object in other pages, I have instanciated my class into the session scope. Now, accessing / setting the variable parts of my session object is fine, no problems. However, I cannot seem to run the methods with in it. Here's an example of what I'm attempting:

<html>
<head>
</head>
<body>
<%!    
	public class testClass
	{
		//my variable
		public String name;

		//my method for updating my variable
		public void updateName()
                   {
                     name="Bill";  
                   }
	
	}
%>

<%

	//create a new object in session
	session.setAttribute("sessionMyObject", new testClass());

	//attempt to run the updateName method in the session object and set 'name' to Bill
	session.getAttribute("sessionMyObject.updateName()");		

	//Display 'name' variable in the session object
	out.print("Name value in session object: "+ session.getAttribute("sessionMyObject.name"));

	%><br/><%

	//accessing the specific variable (and not the methods) DOES work:
	session.setAttribute("sessionMyObject.name", "Lee");		
	out.print("Name value in session object: "+ session.getAttribute("sessionMyObject.name"));
	

	session.invalidate();
%>
</body>
</html>

This runs without any grumbling but the outcome is as follows:

Name value in session object: null
Name value in session object: Lee

Is it because I'm not calling the session object's method correctly, or is it because you cannot achieve what it is I'm trying to do?

Any ideas, as it's driving me mad!

Thanks in advance,

Lee

Recommended Answers

All 6 Replies

What, no one? :(

Hi Friend..
use the following code instead..

<html>
<head>
</head>
<body>
<%!
	public class testClass
	{
		//my variable
		public String name;
	       //my method for updating my variable
		public void updateName()
		{
			name="Bill";
		}
	}
%>
<%
	//create a new object in session
	session.setAttribute("sessionMyObject", new testClass());

	//attempt to run the updateName method in the session object and set 'name' to Bill
	((testClass)session.getAttribute("sessionMyObject")).updateName();
 	//Display 'name' variable in the session object
	out.print("Name value in session object: "+((testClass)session.getAttribute("sessionMyObject")).name);

%><br/><%
 	session.setAttribute("sessionMyObject.name", "Lee");
	out.print("Name value in session object: "+ session.getAttribute("sessionMyObject.name"));
	//session.invalidate();
%>
</body>
</html>

Feel free to contact at <EMAIL SNIPPED>

commented: Don't give wrong advices -3
commented: Java class do not belong to JSP that is only page view. Leave classes for servlet to handle. Secondly do not down rep others for your lousy coding -4

Don't give wrong advices, just so you can make a post and display your mail.

commented: Execute the Code before writting comments.. +0
commented: Equalizer +16

I hope you had executed my Code before putting this reply to my post..

I hope you had executed my Code before putting this reply to my post..

Just because some code executes, doesn't make it right! That is not the right way to use the session or write jsps in general. Putting the class declaration in the JSP = VERY BAD

And you had to go and find a 6 month thread to make a post.

Just because some code executes, doesn't make it right! That is not the right way to use the session or write jsps in general. Putting the class declaration in the JSP = VERY BAD

And you had to go and find a 6 month thread to make a post.

OK i agree with you that it is not the right way to use the session or write in jsps in general.
But that does not mean you would flag my reply wrong as a whole. Just see what "lee" has asked exactly.
I am not here to tell people how to write code.. because if someone is here then he/she must have the strong foundation in terms of coding..
I just told him that where his code was going wrong.. What exactly should be the syntax that could make the program to call "updateName()" method of that class.....
So next time before you flag something wrong.. Watch out what exactly the question in that thread is for...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.