Home Jsp Jspsession Duplicated Session Variables
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Duplicated Session Variables

Advertisement
We don't have any need to create a new session variable because it has been created by the JSP container for you.

Duplicated Session Variables

        

We don't have any need to create a new session variable because it has been created by the JSP container for you. Now consider a case where you want to use the another variable by the same name, for e.g. we want to use "session"  for two times with the same name. Consider we have declared a session variable true in jsp page <%@ page session = "true"> and we want to use it again by using <% HttpSession session = request.getSession();%>. You will get an error saying "Duplication of local variable" because we can't declare a same variable twice.

Now consider a case we want to use the same variable name. At this situation we can solve this problem by using the <jsp: useBean> standard action tag.

The code of the program is given below:

package Mybean;
public class DuplicatedSessionJavaBean{
	private String name;
	private String pwd;
	private String email;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
	public void setPwd(String pwd){
		this.pwd = pwd;
	}
	public String getPwd(){
		return pwd;
	}
	public void setEmail(String email){
		this.email = email;
	}
	public String getEmail(){
		return email;
	}
}

 

<jsp:useBean id="duplicatedSession" class=
"Mybean.DuplicatedSessionJavaBean" scope="session"> <%-- The setProperty tag is only executed when a JavaBean is created --%> <jsp:setProperty name=
"duplicatedSession" property="name" value="James" /> <jsp:setProperty name=
"duplicatedSession" property="pwd" value="xyx" /> <jsp:setProperty name=
"duplicatedSession" property="email" value=" jamesbond@rediffmail.com" /> </jsp:useBean> <html> <head> <title>When a JavaBean already exists...</title> </head> <body> The username is :<jsp:getProperty name=" duplicatedSession" property="name" /><P> The password is :<jsp:getProperty name=" duplicatedSession" property="pwd" /><P> The email is :<jsp:getProperty name ="duplicatedSession" property="email" /><P>
    <a href="DuplicatedBean.jsp"><h1>Click
 </h1></a><br> to 
see another page that has the use <br> the same JavaBean with same name and scope. </body> </html>

 

<jsp:useBean id="duplicatedSession"   class="
Mybean.DuplicatedSessionJavaBean"
             scope="session" />
<html>
  <head>
    <title>When a JavaBean already exists...</title>
  </head>
  <body>
    <h1>Java Bean is already exist by this name and scope</h1>
    <P>
    The userName is :<jsp:getProperty 
name="duplicatedSession" property="name" /><P>
	The password is :<jsp:getPropert
y name="duplicatedSession" property="pwd" /><P>
	The email is    :<jsp:getPropert
y name="duplicatedSession" property="name" /><P>
  </body>
</html>

The output of the program is given below:

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 22, 2009

Ask Questions?    Discuss: Duplicated Session Variables   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com