0

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

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by kumarvishal_vk
-2

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>

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Votes + Comments
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
Don't give wrong advices
0

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.

0

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...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.