Share on Google+Share on Google+

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.

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){ = name;
	public String getName(){
		return name;
	public void setPwd(String pwd){
		this.pwd = pwd;
	public String getPwd(){
		return pwd;
	public void setEmail(String 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=" [email protected]" /> </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="
             scope="session" />
    <title>When a JavaBean already exists...</title>
    <h1>Java Bean is already exist by this name and scope</h1>
    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>

The output of the program is given below:

Download this example.



Posted on: May 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Duplicated Session Variables   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image