Solved

about remember login in JSP

Posted on 2003-11-13
3
996 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
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link a File Housed on a Shared Drive to a repository in SharePoint 2013 6 27
CSS Logo Problem. 2 21
Python - desktop use 1 32
parse url to form? 7 14
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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

822 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