469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

How to Retain the UserName text box ?

Hi All

I am new to JSP Js and Web

I need to retain my Username text in my text box when the user types the Wrong username and password

I tried several methods.. I didn get the solution.. I finally sent the username in url to get it back

Is there a way to retain the username text after the data being posted?

Please suggest me some details and examples..

Thanks in Advance
Vignesh.M
Apr 4 '09 #1
13 9782
dmjpro
2,476 2GB
See when you are authenticating something then you must need to use POST method. You use a JSP or HTML for log in screen then Post the data for authentication to a action JSP page or Controller then forward back the same view page(what you used for login screen) so that you will show the user name in the text with some error messages.
Did you get it ?
Apr 4 '09 #2
@dmjpro
I do whatever you said ... In php we have a coding option like
<code>
<input type="text" name="username" value="<?php echo @$_POST['username']; ?>" />
</code>

There is some option in FTL also

Is there any option in JSP files to get back the post values like the above code in php where we get the value of the post...

Did you get my question?

I am struggling with it.
Can anyone help?
-
Thanks
Vignesh.M
Apr 4 '09 #3
Can anyone help me out in this issue

I ve got only one response so far...

Is it impossible in JSP?

Can anyone get me a method to get the prevoius POST values as we get in php and ftl or so

Pl help me out

-
Vignesh.M
Apr 6 '09 #4
dmjpro
2,476 2GB
I am not familiar with PHP so don't know about FTL ;)
Actually your forwarded page should be the login page if authentication fails. Otherwise it will be something else. When you forwarding the login page on authentication failure then you simply place the user name by "request.getParameter("userName")" ;)
Apr 6 '09 #5
@dmjpro
Hi ,

Always the response.getParameter("ANYItem"); is returning null
For example i have a hidden field Its name is"h" and id is "h"

i give request.getParameter("h");

It always returns me NULL

What could be the reason... If that had worked i would ve solved my issue but that failed ... What could be the reason for the returning of NULL value in response.getParameter(); I think of a reason.. I post some POST data to server and the server responds, If the credentials are wrong then the its redirectd to the login Page itself. While resending the old POST data sent from server might be lost. Am i right or not? If not pl correct me.

I really don know why i m getting NULL values in the getParameter call

Thanks
-
Vignesh.M
Apr 6 '09 #6
r035198x
13,262 8TB
Did you use RequestDispatcher.forward? Set the values as attributes in the servlet using
Expand|Select|Wrap|Line Numbers
  1. request.setAttribute("h", value);
then get the value in the jsp using
Expand|Select|Wrap|Line Numbers
  1. request.getAttribute("h");
Apr 6 '09 #7
@r035198x
I redirect to my login page using ServeletActioncontext.getResponse.sendRedirect("lo ginPage");
from my java class

How to set the attribute of h here ..

My loginpage is a jsp file..called login.jsp from there i can get my attribute as request.getAttribute("h"); but i couldn get u where to set it...

I am new to Web
Thats why

-
Vignesh.M
Apr 6 '09 #8
i think you should read this book; http://pdf.moreservlets.com/ All the issues JSP / Servlet are explained in great detail.
Apr 6 '09 #9
r035198x
13,262 8TB
You set it in the servlet you are posting to. However, when you sendRedirect, a new request is created so the data you set in the request will no longer be available. You can store the values as attributes in the session if you want them to persist across multiple requests.
Apr 6 '09 #10
dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. <form name=loginForm action="some_servlet or JSP" method="POST">
  2. <input type=text name=userName value="<%=request.getParameter("userName")!=null?request.getParameter("userName"):""%>"/>
  3. <input type=password name=password/>
  4. </form>
  5.  
That would be your form code..you submit the form and return back the same page if authentication fails no need to add any attribute into "request" scope.

And don't use "sendRedirect" why are you redirecting the instead of forwarding the page?
Apr 6 '09 #11
@dmjpro

Hi

Is using sendRedirect the cause of problem?

Hoe to do the forwarding from my java code...

Which class gives the method of "forwarding" instead of redirecting?

-
Thanks for the help

Vignesh.M
Apr 7 '09 #12
dmjpro
2,476 2GB
Send redirect causes a new request but forward does not. So you will be still able to access the "request" object.
If you use JSP then use "jsp:forward" tag otherwise use "RequestDispatcher".
Look for google ;)
Apr 7 '09 #13
JosAH
11,448 Expert 8TB
@VigneshMohan
See this:

Expand|Select|Wrap|Line Numbers
  1. // at the end of your doGet/doPost method:
  2. getServletContext().getRequestDispatcher("/jsps/yourjsp.jsp").forward(request, response);
  3.  
Note that you can pass the same request and response objects. You can set new values in them as well and they are passed (forwarded) to your jsp. One of those values should be the username so the login jsp can set and display it.

kind regards,

Jos
Apr 7 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Renuka | last post: by
2 posts views Thread by Joebloggs | last post: by
1 post views Thread by Andy G | last post: by
3 posts views Thread by Dhruba Bandopadhyay | last post: by
2 posts views Thread by =?Utf-8?B?U2hydXRpIERlc2hwYW5kZQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.