Solved

about remember login in JSP

Posted on 2003-11-13
3
1,004 Views
Last Modified: 2008-02-01
Hi There,
I have made a JSP login page in which Iwant to make a Remember Me check box .
If any user comes again my Username text box will display its username automatically.
Can anybody help me or give me any idea about it.
If possible with some example code.
Thanks in advance.
0
Comment
Question by:m_sharma98
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 20 total points
ID: 9785957
You have to use a cookie for this.

something like:


with JSP
  To set the cookie
 
    with JSP
      <%
        if (request.getParameter("rememberMe") != null) {
          Cookie myCookie = new Cookie("username", request.getParameter("username"));
          //myCookie .setMaxAge(24 * 60 * 60);
          response.addCookie(myCookie );
        }
    %>


  To read the cookie

  <%
    String username;
    Cookie [] cookies = request.getCookies();
    for( int i =0 ; i < cookies.length ; i++){
      if(cookies[i].getName().equals("username")){
         username = cookies[i].getValue();              
      }
    }
  %>

  and to put it on the screen

  <input type="text" name="username" value="<%=username%>">



Or with Javascript

  <script>
    function setCookie(name, value, expires, path, domain, secure){
      document.cookie= name + "=" + escape(value) +
          ((expires) ? "; expires=" + expires.toGMTString() : "") +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          ((secure) ? "; secure" : "");
   }

   function getCookie(name){
      var dc = document.cookie;
      var prefix = name + "=";
      var begin = dc.indexOf("; " + prefix);
      if (begin == -1)
      {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
      }
      else
      {
        begin += 2;
      }
      var end = document.cookie.indexOf(";", begin);
      if (end == -1)
      {
        end = dc.length;
      }
      return unescape(dc.substring(begin + prefix.length, end));
    }

    function gotoOK(){
      setCookie(username, document.all.username.value);
      document.yourForm.action="yourjsp.jsp";
      document.submit();
    }

  </script>

  and then on you form a button to do your submit
 
  <input type="button" name="myButton" value="OK" onclick="javascript:gotoOK();">


  <input type="text" name="username" value="<script>getCookie(username)</script>">


I can not test all the code here, so you might have to change some things. But this should give you an idea how to do it.

Greetz,
Didier
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't get cursor to change 3 21
Validating number not work with decimal 4 46
Embedd an image or live dashboard into Slack 1 55
Why is my select returning NaN 23 40
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question