Escolar Documentos
Profissional Documentos
Cultura Documentos
If (session=null)
IF COOKIES ARE DISABLED CLIENT WILL REJECT SET COOKIE RESPONSE
HEADER
Whats the way out if the client doesnt accept cookies?
URL rewriting.
All you need is that the client and container are able to exchange session id info.
URL rewriting takes the session id thats in the cookie and sticks it right onto the end of
every URL that comes in to this app.
Cookies
Cookies can be used for other things besides session like remembering client name.
1. Cookie exchange is automatic
2. Cookie lives as long as session, once client quits his browser, cookie
disappears.
3. Thats how the jsessionid cookie works
4. But you can tell a cookie to stay alive even after the browser shuts down.
Using cookies with the servlet API
You can get cookie related info from request and response headers but dont.
Everything related to cookies has been encapsulated in the Servlet API in 3 classes :
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
javax.servlet.http.Cookie
Creating a cookie:
Cookie cookie = new Cookie(username,name);
Setting how long a cookie will live on the client:
cookie.setMaxAge(30*60);
Sending the cookie to the client :
response.addCookie(cookie);
Getting the cookies from the client request :
Cookie[] cookies = request.getCookies();
for(int i=0;i<cookies.length;i++){
Cookie cookie= cookies[i];
if(cookie.getName.equals(username)){
String username = cookie.getValue();
out.println(Hello+username);
break;
}
}