This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes sendRedirect with passing some values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect with passing some values." Watch "sendRedirect with passing some values." New topic

sendRedirect with passing some values.

Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

I'm using a servlet to check whether the userName and password are exist if it is false I have to redirect to the login page with some message like "Invalid user name or password".
Now I used session.setAttribute("error",msg);
Is there any other way to pass error message to login page.

Java Best Practices
Linux Best Practices
Amortization Calculator

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can use a request parameter for that. You can just do a forward instead of redirect.
Steve Luke

Joined: Jan 28, 2003
Posts: 4167

You can pass the error to the next page as a url parameter:
String url = "/login?error="+URLEncoder.encode("Invalid Username or Password", "UTF-8");
The URLEncoder is needed to encode spaces so they don't break the URL.

A way to make passing the parameter easier is to store the possible errors in a central location with a lookup table. You would use a basic lookup value (like an integer) as a key for the lookup value, which simplifies the URL from:

Here is an example below. Note that I don't have the webapp where I used this with me and I haven't done servlet/jsp programming in a few months so I am likely making mistakes, so take it as a guideline.

Central store for the error messages

Somewhere early in the webapp, like a context listener

In the Servlet that detects the error and does the forward

In the JSP that displays the login form:

[ December 19, 2008: Message edited by: Steve Luke ]

Knaresh Gupta

Joined: Nov 22, 2008
Posts: 5
Hello you can simple write the HTML Code in else part like this
out.println("<li><i>we con't find the username/password provided by you</i></li>");

it simply display the message in browser and automatically come back.
I agree. Here's the link:
subject: sendRedirect with passing some values.
Similar Threads
not showing the message
submit blank fields
Problem with JDBCRealm configuration
conditionally targetting the client frame based on Server(servlet) program
need help with declarative security