This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, 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.
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: /login?error=Invalid+Username+or+Password to /login?error=1
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