Solved

about remember login in JSP

Posted on 2003-11-13
3
1,005 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

624 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