AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problema con i cookies nella servlet

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
rastamc
Guest





PostPosted: Wed May 17, 2006 11:08 am    Post subject: Problema con i cookies nella servlet Reply with quote



ho un problema con i cookies, praticamente non riesco ad inviarli al
browser..
la servlet mi stampa a video sul log del tomcat "cookies inviati!" dove
ho messo la system.out per il debug, ma poi non trovo i file nella
cartella dei cookies del browser, e soprattutto quando invoco il metodo
getCookies ottengo qualcosa.. ottengo sempre un vettore vuoto, un null
pointer...

cosa ho sbagliato?


ecco il codice della servlet:

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import operazioni.FactoryFunction;
import operazioni.Function;
import beans.LoggedUser;

public class FrontController extends HttpServlet
{


public void service(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException
{

try
{
System.out.println("Inizio metodo service FrontController");
HttpSession session = req.getSession(true); // Ricava la sessione
del LoggedUser
LoggedUser loggedUser=(LoggedUser)
session.getAttribute("LoggedUser");
if (loggedUser==null)
{
System.out.println("ricerca cookies");
loggedUser = new LoggedUser();
Cookie[] cookies=req.getCookies();

loggedUser.setUsername(getCookieValue(cookies,"username","ospite"));
loggedUser.setEmail(getCookieValue(cookies,"email",""));

loggedUser.setRole(getCookieValue(cookies,"role","guest"));
System.out.println(loggedUser.getUsername());
session.setAttribute("LoggedUser", loggedUser);
}
String op = req.getParameter("operation");
String daInoltrare=null;
if(op!=null){
Function f = FactoryFunction.getInstance(op);
daInoltrare=f.execute(req);
System.out.println("executed ok! : " +op);
System.out.println("daInoltrare : "
+daInoltrare);
//Forward alla jsp, nella cartella sotto jsp il contesto

getServletContext().getRequestDispatcher("/"+daInoltrare).forward(req,res);

}

if(op.equals("operazioni.Login")||op.equals("operazioni.Logout"))
{
session = req.getSession(true); // Ricava la
sessione del LoggedUser
loggedUser=(LoggedUser)
session.getAttribute("LoggedUser");
Cookie biscotto=new
Cookie("username",loggedUser.getUsername());
biscotto.setMaxAge(864000);
biscotto.setPath("/");
res.addCookie(biscotto);
biscotto=new
Cookie("role",loggedUser.getRole());
biscotto.setMaxAge(864000);
biscotto.setPath("/");
res.addCookie(biscotto);
biscotto=new
Cookie("email",loggedUser.getEmail());
biscotto.setMaxAge(864000);
biscotto.setPath("/");
res.addCookie(biscotto);
System.out.println("Cookies spediti a
"+loggedUser.getUsername()+".");
}
}
catch(Exception e)
{
System.out.println("Eccezione Front Controller:"+e.getMessage());
e.printStackTrace();
}
}

public String getCookieValue(Cookie[] cookies,String cookieName,
String defaultValue)
{
if (cookies!=null)
{
for(int i=0; i<cookies.length; i++)
{
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
{
System.out.println("COOKIE trovato valore:
"+cookie.getValue());
return(cookie.getValue());

}
}
}
else System.out.println("Nessun cookie trovato sul client!");
return defaultValue;
}
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.