1

OK, I'm kind of inept at explaining my problems, but I'll try to be as detailed, yet concise as possible.

I have 2 servlets; NewCustomerServlet and LoginServlet

I have 1 Java bean; User

The User has a bunch of fields. username, firstName, password, etc...

my index.jsp automatically directs the user to NewCustomerServlet, so the user can create an "account" Once they finish filling in the fields the User bean is created and saved in the session. Then, the user is able to "login"

The problem I'm having is validating the username from the "user" session with the login.jsp fields using the session.

How do I access the sessions "username" or "password" field. All I can seem to access is the name of the session, which would be "user"?

1 Answer 1

1

Referencing the JavaDoc for HttpSession,

You will realize that HttpSession stores attributes by key, just like a HashMap, you would place objects in the session (Any serializeable object) like:

String userName = "something";//
session.setAttribute("username", userName);

Then you can get it back out using:

String un=(String)session.getAttribute("username");  

It is possible to store more complex objects, like an entire User object, as long as that object implements Serializable:

User someUser = //details left to the OP
session.setAttribute("user", someUser);

Then you can later retrieve the information for that user:

User someUser = (User)session.getAttribute("user");
if(user != null){
    String username = user.getUsername();
}

Also, here is a nice, brief run-through of common session utilization with Servlets and HttpSession: Session Tracking

Sign up to request clarification or add additional context in comments.

1 Comment

Jesus... I've been "learning" Java for about 2 years now, and one thing that slipped my mind for the project was being able to; User someUser = (User)session.getAttribute("user"); That was the key that I was missing. I forgot to mention that User IS serializable and I apologize for that. All in all, this was exactly what I needed, and THANK YOU for the concise information!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.