user99244
user99244

Reputation: 13

Showing error message on same page with a Session in JSP?

I have this situation where I am required to show where if user does not enter right password, the program will place an appropriate message in a session & will Dispatch back to the login jsp page and display the message.

I checked this one as well, Redirect to the same page but with a message in it

But when I try implement the answer, the eclipse gives me a red line over the code.

Here is my code,

String message = "Wrong Password!";

HttpSession session = session.setAttribute("message", message);

The error shown is Type Mismatch: Cannot Convert to from void to HttpSession

Any ideas where I am wrong or how can I do this?

Upvotes: 0

Views: 5772

Answers (4)

Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

HttpSession session = session.setAttribute("message", message);

Above line is wrong. you need to do just a...

session.setAttribute("message", message);

That's it. And on jsp page while getting session value.

out.println(session.getAttribute("message"));

or

<%=session.getAttribute("message")%>

Upvotes: 0

NPKR
NPKR

Reputation: 5506

session.setAttribute("message", message);

this return type is void, hence your pionting to HttpSessoin Object, so it is giving showing complier error.

Use this

HttpSession session = request.getSession(true);
session.setAttribute("message", message);

Upvotes: 0

Renjith
Renjith

Reputation: 3274

Simply the setAttribute() method wont return a session object.

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19294

session.setAttribute() returns "void" and not "HttpSession".

You need to get the session from the request and then use setAttribute() on the session attribute.

Upvotes: 1

Related Questions