Solved

about remember login in JSP

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now