Problem with session in the servet

2

I have the saida.jsp . It is my page that depending on the option chosen by the user, it is mounted one way. I have two options for user verbs and adverbs . If the user clicks the verbs option it will be redirected to the saida.jsp page and it will be mounted one way. The same goes for the adverbs option. My problem is when the user selects the verbs option, then he turns the page and selects the adverbs option. When this happens, the adverbs page appears as if it were verbs . I've already tried it, they told me it's a session error, but I'm not much of a session savvy.

if(str.equals("verbos")){
            extrair = Extrair.verbos();
            verbos.contVerb = extrair.contverb;
            request.getSession().setAttribute("verbos", verbos);
            rd = request.getRequestDispatcher("saida.jsp");
}
else if(str.equals("adverbios")){
            extrair = Extrair.adverbios();
            adverbios.contAdv = extrair.contadv;
            request.getSession().setAttribute("adverbios", adverbios);
            rd = request.getRequestDispatcher("saida.jsp");
}

This is the treatment you said. I do not know how to mess with a session, could anyone clarify?

    
asked by anonymous 20.01.2015 / 20:00

1 answer

3

You are putting the attributes in the session, but one is not cleaning the other. When accessing the two pages, both "adverbs" and "verbs" attributes will have been set in the session and both will be visible in saida.jsp , since the request will receive the session the way the previous request left it.

The workaround is to remove the unwanted attribute:

if(str.equals("verbos")){
        extrair = Extrair.verbos();
        verbos.contVerb = extrair.contverb;
        request.getSession().setAttribute("verbos", verbos);
        request.getSession().setAttribute("adverbios", null); // Remove o "adverbios".
        rd = request.getRequestDispatcher("saida.jsp");
}
else if(str.equals("adverbios")){
        extrair = Extrair.adverbios();
        adverbios.contAdv = extrair.contadv;
        request.getSession().setAttribute("adverbios", adverbios);
        request.getSession().setAttribute("verbos", null); // Remove o "verbos".
        rd = request.getRequestDispatcher("saida.jsp");
}

Incidentally, this is a remake from your other question . Since you wrote this one out much better, it was much easier to answer. :)

    
20.01.2015 / 22:00