File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP session" Watch "JSP session" New topic
Author

JSP session

Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Hello,
Please could someone guide me as to what is wrong with the following code:

EmpBean.jsp
/*
* EmpBean.java
*
* Created on December 31, 2002, 10:23 AM
*/
import java.beans.*;
/**
*
* @author Administrator
*/
public class EmpBean extends Object implements java.io.Serializable
{

private String fname;
private String lname;
private String salary;
/** Creates new EmpBean */
public EmpBean()
{
System.out.println("Creating a new bean");
}

public String getFname()
{
return fname;
}

public void setFname(String value)
{
System.out.println("Storing FName"+value);
fname = value;
}

public String getLname()
{

return lname;
}

public void setLname(String value)
{
System.out.println("Storing LName"+value);
lname = value;
}

public void setSalary(String value)
{
System.out.println("Storing salary value"+value);
salary = value;
}

public String getSalary()
{
return salary;
}


}
--------------
EmpData.jsp
<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>
<jsp:useBean id="empData" scope="session" class="EmpBean" />
<jsp:setProperty name="empData" property="*" />

<FORM METHOD="POST" ACTION="/genericacg/Display.jsp">
FirstName<input type="text" value="" name="fname"><br>
Last Name <input type="text" value="" name="lname"><br>
<INPUT TYPE="SUBMIT" VALUE="SENT" NAME="GO" >
</FORM>
</body>
</html>
---------------
Display.jsp
<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>
<jsp:useBean id="empData" scope="session" class="EmpBean" />
<jsp:setProperty name="empData" property="*" />
<%= empData.getFname() %><br>
<%= empData.getLname() %><br>
<FORM METHOD="POST" ACTION="/genericacg/Salary.jsp">
Salary<input type="text" value="" name="salary"><br>
<INPUT TYPE="SUBMIT" VALUE="SENT" NAME="GO" >
</FORM>
</body>
</html>
-------------
Salary.jsp
<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>
<jsp:useBean id="empData" scope="session" class="EmpBean" />
<jsp:setProperty name="empData" property="*" />
<%= empData.getFname() %><br>
<%= empData.getLname() %><br>
<%= empData.getSalary() %><br>
</body>
</html>
---------------
Now my starting point is EmpData.jsp
When I click on the "submit" button on this page i get Display.jsp which shows the Employee Fname and last name and collects info about the salary.
When I click on te "submit" on Display.jsp I get Salary.jsp.
Which shows the FName and LName as null but shows the Salary entered in Display.jsp.
My problem is even though I maintain the scope of the bean as "session" it does not persist across different jsp pages.Instead a new bean is created when I access a diff jsp page.
I want the bean data to persist during a session.Is my method of doing it incorrect?Could someone guide me towards the right way of doing this.
Thanks
Ruplai Desai
Ruchi Kolla
Greenhorn

Joined: Jan 24, 2002
Posts: 19
In your salary.jsp page remove <jsp:setproperty> tag and try it again.
Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
No That didnt help. It basically creates a new bean when a page is accessed instead of maintaining the bean through out the session.
Thanks for the suggestion
Rupali
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Please could someone guide me as to what is wrong with the following code:

EmpData.jsp

Well, this is not the problem you were asking about, but one very serious problem with the above is the lack of packages. It is not guaranteed to work if you use packageless beans with jsp:useBean. Even worse, by luck it sometimes does work.
Here is a question: in general in Java, when you refer to a class and use neither a fully scoped name (ie with packages included) nor import statements, what package does the system think that class is in?
Answer: the same package as the current class!
Here is another question: when a JSP page gets translated into a servlet, what package will it be in?
Answer: unknown!
See the problem? So, a packageless bean will work only if the servlet happens to also be packageless, but there is no standard that governs this. Most versions of Tomcat generate packageless servlets for JSP pages that are in the top-level directory of the Web app, but use packaged servlets for JSP pages that are in subdirectories. But that is just how the current Tomcat implementation happens to work: this is totally unspecified by the spec.
Let me say this as clearly as I can, since failing to do so causes all sorts of problems: Always, always, always put all beans and utility classes in packages. When using a bean from JSP, use
<jsp:useBean ... class="packageName.BeanName".../>
.
Cheers-
- Marty


Java training and consulting
Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Thanks for the reply.I will try it out with packages.
Thanks
Rupali desai
Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
I tried it out with packages but even that doesnt seem to work.Only when i set the scope to application is a new bean not created.
Rupali Desai
Piyush Daiya
Ranch Hand

Joined: Jun 13, 2002
Posts: 67
Hi,
The code is working fine with me.I have OC4J and jdk1.3.1_04.
What web container are u using?
Only changes i made in code are :-
changed form action in EmpData.jsp to
<FORM METHOD="POST" ACTION="Display.jsp">
and
changed form action in Data.jsp to
<FORM METHOD="POST" ACTION="Salary.jsp">
as all files are in same directory.But, it shouldnt matter.
HTH,
Piyush


"A scientist is not person who gives right answers but a person who asks right questions"
Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Hello,
Well I got it to work with Tomcat 3.2.For some reason it never worked with tomcat 3.3.I even tried reinstalling toncat 3.3.
I am going to try an dtest with tomcat 4.1.
But anyone it works fine with tomcat 3.2.
Thanks for all the help.
Rupali Desai
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP session