Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: using cookies


Message #1 by "matt" <mattslists@o...> on Wed, 27 Dec 2000 15:47:25 -0000
Hi,

Create a stand alone jsp file with the following code, then include the jsp 
in the other jsp where u can create cookies and getcookies value for other 
usage.

-------codes to be in other jsp file to get cookie value-----------
String tmp_id = getCookie(request, "id");
String tmp_password = getCookie(request, "password");

----------- codes to be in the stand alone jsp file-----------------
<%!
public void createCookie( HttpServletResponse response, String name,
                          String value, int expires){
  Cookie c = new Cookie(name, value);
  c.setMaxAge(expires);
  c.setPath("/");
  response.addCookie(c);
}

public String getCookie( HttpServletRequest request, String cookieName){
  Cookie c[] = request.getCookies();
  if(c != null){
    for(int i = 0; i < c.length; i++){ //(1)
      if(c[i].getName().equals(cookieName)){ //(2,3)
      return c[i].getValue(); //(4)
    }
  }
}
return null;
}
%>

Hope this can help!

regards,
James :-)

----------------precious email------------------------
>From: "matt" <mattslists@o...>
>Reply-To: "Pro_JavaServer_Pages" <pro_jsp@p...>
>To: "Pro_JavaServer_Pages" <pro_jsp@p...>
>Subject: [pro_jsp] using cookies
>Date: Wed, 27 Dec 2000 15:47:25 -0000
>
>im a cookie newbie.  from what i understand, if you want to set a cookie,
>you have to send it, before any html is written.  how can i put a form
>submitted username into a cookie and set it with in the same page?
>
>

--- 
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development, 
Web Development, Networking & Communications, and Hardware & Systems.  
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to pro_jsp as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-pro_jsp-$subst('Recip.MemberIDChar')@p2p.wrox.com

  Return to Index