
Remember me on this computer (via cookies) in jsp servlet

Hi,
Here is the detail of using cookies to identify the user.
In Servlet or JSP you can set the cookies for a user and then add it to the response object.
Here is the code of setting the cookies:
<%@ page language="java" import="java.util.*"%>
<%
// Generate this key And save In database And Set as cookies
String userIdendificationKey="dxkdyneimd@5786*54904";
Date now = new Date();
String timestamp = now.toString();
Cookie cookie = new Cookie ("userIdendificationKey",userIdendificationKey);
//Set the required cookies age
cookie.setMaxAge(365 * 24 * 60 * 60);
//Then add the cookies
response.addCookie(cookie);
%>
You can read the cookies and idenfity the user. Here is the sample code:
<%
String userIdendificationKey = "";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals ("userIdendificationKey"))
{
userIdendificationKey = cookies[i];
break;
}
}
}
//Then use the userIdendificationKey And Get the user name from database
//To identify the user
//Do whatever you want after identifying the user
%>
Let me know in the same post if you have any query.
Thanks

hi,
Then what will happen if the customer deletes the cookies from browser menu ???
My requirement is to set the cookie in clients machine and retrieves whenever it needs for the application..
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.