Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

about remember login in JSP

Posted on 2003-11-13
3
Medium Priority
?
1,010 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 80 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

Technology Partners: 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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

926 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