Click to See Complete Forum and Search --> : HttpSession session help


Shaolin
08-28-2008, 11:43 AM
Hi Guys

I have a session bean, I set some method. All is fine until I want to retrieve the value of the session. See code below followed by error message:

package beans;

public class UserSession
{
private int user_id;

public UserSession()
{
user_name = 0;
}

public void setFirstName(str nm) {
user_name = nm;
}

public String getFirstName() {
return user_name;
}
}

setSession servlet:
public class SetSession extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

UserSession user; = new UserSession();
user.setFirstName("test");

HttpSession session = request.getSession();
session.setAttribute( "UserSession", user );
}
}

getSession servlet:
public class getSession extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

HttpSession session = request.getSession();
UserSession user = (UserSession) session.getAttribute( "UserSession" );

out.println(user.getFirstName());
}
}

error:
getSession.java:13: getFirstName(java.lang.String) in beans.UserSession cannot be applied to ()
out.println(user.getFirstName());
^

chazzy
08-28-2008, 12:34 PM
It looks like you have an out of date version of UserSession on your classpath, and the servlet is trying to compile against that. try recompiling the whole thing, not just the class, and see if that helps any.

Shaolin
08-28-2008, 01:07 PM
That didn't work.

Please note that in UserSession the user_name in the constructor should be:

user_name ="";

Shaolin
08-28-2008, 01:36 PM
Ah, I managed to fix the problem :D

chazzy
08-28-2008, 02:46 PM
mind sharing what it was?

Shaolin
08-28-2008, 03:12 PM
I had a variable between the brackets of one of the getter methods.