• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sendRedirect with passing some values.

 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a request parameter for that. You can just do a forward instead of redirect.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

In the JSP that displays the login form:

[ December 19, 2008: Message edited by: Steve Luke ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>");
return;

it simply display the message in browser and automatically come back.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic