java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3946
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Using cookies from JSP E-mail
User Rating: / 54
PoorBest 

Sometimes there is a need to access cookies on a client-machine to save some data. JSP represents cookies with a Cookie object. A cookie is normally composed of several attributes. The most important data is the name-value pair, which is the data the server would retrieve from the cookie. But the cookie also contains information on when to expire the cookie and when to give out the cookie.

The sample page below shows an example of getting and setting cookies. This uses cookies to keep the name of the user loggen in:

<%@ page import ="java.util.*">
<%!
  /** this function converts an array of cookies into a hashtable */
  Hashtable cookieTable(Cookie[] cookies) {
    Hashtable cookieTable = new Hashtable();
    if (cookies != null) {
      for (int i=0; i < cookies.length; i++)
        cookieTable.put(cookies[i].getName(), cookies[i].getValue());
    }
    return cookieTable;
  }
%>
<%
  Cookie myCookie;
  String username = new String();
  Hashtable cookies = cookieTable(request.getCookies());

  String newLogin = request.getParameter("name");
  if (cookies.containsKey("name")) {
    username = (String)cookies.get("name");
  }

  if (newLogin != null) {
    if (newLogin.equals("")) {
      myCookie = new Cookie("name""");
      myCookie.setMaxAge(0);
      username = null;
    else {
      myCookie = new Cookie("name", newLogin);
      myCookie.setMaxAge(3600);
      username = newLogin;
    }

    myCookie.setDomain(".somedomain.com");
    response.addCookie(myCookie);
  }
%>

<html>
  <body bgcolor="#ffffff">

  <% if (username == null || username.equals("")) { %>

    You look like a new user! <br />
    <form method="get" action="<%= request.getRequestURI()%>">
      Enter your name to create login : <br />
      <input type="text" size="8" name="name" />
      <input type="submit" value="login" />
    </form>

  <% else %>

    Welcome back, <%= username %> <br />
    <%= new java.util.Date() %>

    <form method="get" action="<%= request.getRequestURI()%>">
      Enter your name to create login : <br />
      <input type="hidden" name="name" value=""  />
      <input type="submit" value="logout" />
    </form>

  <% %>
  </body>
</html>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.