codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



JSP Easy Cookies Script



Here's a simple script to set a cookie in JSP.


 


<%@ page import="java.util.Date"%>
<%@ page import="java.net.*"%>
<% 
	
	Date now = new Date();
	String timestamp = now.toString();
	Cookie cookie = new Cookie ("Username", "David");
	cookie.setMaxAge(365 * 24 * 60 * 60);
	response.addCookie(cookie);
%>
<HTML>
<HEAD>

</HEAD>

<BODY>
 

<%=cookie.getValue()%>

</BODY>
</HTML>





Now here's how you check whether it exists or not.


<%@ page import="java.net.*"%>
<% 
	   String cookieName = "Username";
	   Cookie cookies [] = request.getCookies ();
	   Cookie myCookie = null;
	   if (cookies != null)
	   {
	      	for (int i = 0; i < cookies.length; i++) 
		      {
			         if (cookies [i].getName().equals (cookieName))
			         {
				            myCookie = cookies[i];
				            break;
			         }
		      }
	   }
%>
 
<HTML>
<HEAD>

</HEAD>

<BODY>
 

<%if (myCookie == null) {%>
Hello first timer!
<%} else {%>	
Hi again <%=redCookie.getValue()%>, welcome back!
<%}%>
</BODY>
</HTML> 

 











Recent Forum Threads
• C++
• Re: refresh parent after closing pop up window
• Dynamic Insertion
• Date and Time function around the world???
• Significant Factors
• Perl array access
• Re: huffman encoding and decoding in C++...
• Perl One Liner: Replace {(
• Re: html including php, accessing the functions


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009