Problem with session in Servet

Asked

Viewed 124 times

2

I have the saida.jsp. It is my page that depending on the option chosen by the user, it is mounted in one way. I have two options for the user verbs and adverbios. In case the user clicks on the option verbs it will be redirected to the page saida.jsp and it will be mounted in a form. The same goes for the option adverbios. My problem is when the user selects the option verbs, then it goes back to page and selects the option adverbios. When this happens, the page adverbios appears as if it were verbs. I’ve looked into it, I’ve been told it’s a session mistake, but I’m not really a session expert.

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");
}

That’s the treatment I said. I don’t know how to handle session, someone could enlighten me?

  • 1

    Extrair.(); - that does not compile.

1 answer

3


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

The solution 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");
}

By the way, I see this is a remake of your other question. Since you’ve designed this one in a much better way, it’s much easier to answer. :)

  • this actually worked for the reported problem. But now when it loads the second option, it goes blank.. is not passed the values that are in the object.

  • 1

    @Peace Because you use the variable "verbs" in the adverb block?

  • was unintentionally that, already fixed. I was wrong only Ake, my code is right.

  • @Peace Well, whatever’s still wrong now, it doesn’t seem to be in this snippet of code you posted. How’s the JSP? Where do you get the variable from str?

  • that’s the problem. when I put one sout it shows in the IDE the str correct. It recognizes, but when it passes to the.jsp output it cannot pick up the content of the object.

  • @So what’s inside the JSP after all?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.