How to Retain the UserName text box ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VigneshMohan
    New Member
    • Apr 2009
    • 10

    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
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    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 ?

    Comment

    • VigneshMohan
      New Member
      • Apr 2009
      • 10

      #3
      Originally posted by dmjpro
      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 ?
      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

      Comment

      • VigneshMohan
        New Member
        • Apr 2009
        • 10

        #4
        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

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          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.getPar ameter("userNam e")" ;)

          Comment

          • VigneshMohan
            New Member
            • Apr 2009
            • 10

            #6
            Originally posted by dmjpro
            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.getPar ameter("userNam e")" ;)
            Hi ,

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

            i give request.getPara meter("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.getPar ameter(); 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

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Did you use RequestDispatch er.forward? Set the values as attributes in the servlet using
              Code:
              request.setAttribute("h", value);
              then get the value in the jsp using
              Code:
              request.getAttribute("h");

              Comment

              • VigneshMohan
                New Member
                • Apr 2009
                • 10

                #8
                Originally posted by r035198x
                Did you use RequestDispatch er.forward? Set the values as attributes in the servlet using
                Code:
                request.setAttribute("h", value);
                then get the value in the jsp using
                Code:
                request.getAttribute("h");
                I redirect to my login page using ServeletActionc ontext.getRespo nse.sendRedirec t("loginPage" );
                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.getAttr ibute("h"); but i couldn get u where to set it...

                I am new to Web
                Thats why

                -
                Vignesh.M

                Comment

                • N002213F
                  New Member
                  • Sep 2007
                  • 38

                  #9
                  i think you should read this book; http://pdf.moreservlets.com/ All the issues JSP / Servlet are explained in great detail.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    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.

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Code:
                      <form name=loginForm action="some_servlet or JSP" method="POST">
                      <input type=text name=userName value="<%=request.getParameter("userName")!=null?request.getParameter("userName"):""%>"/>
                      <input type=password name=password/>
                      </form>
                      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 "sendRedire ct" why are you redirecting the instead of forwarding the page?

                      Comment

                      • VigneshMohan
                        New Member
                        • Apr 2009
                        • 10

                        #12
                        Originally posted by dmjpro
                        Code:
                        <form name=loginForm action="some_servlet or JSP" method="POST">
                        <input type=text name=userName value="<%=request.getParameter("userName")!=null?request.getParameter("userName"):""%>"/>
                        <input type=password name=password/>
                        </form>
                        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 "sendRedire ct" why are you redirecting the instead of forwarding the page?

                        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

                        Comment

                        • dmjpro
                          Top Contributor
                          • Jan 2007
                          • 2476

                          #13
                          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:forwar d" tag otherwise use "RequestDispatc her".
                          Look for google ;)

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by VigneshMohan
                            Hoe to do the forwarding from my java code...

                            Which class gives the method of "forwarding " instead of redirecting?
                            See this:

                            Code:
                            // at the end of your doGet/doPost method:
                            getServletContext().getRequestDispatcher("/jsps/yourjsp.jsp").forward(request, response);
                            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

                            Comment

                            Working...