Problems with if Else in Servlet

Asked

Viewed 387 times

1

I’m messing with jsp. In mine servlet have if and else if and they both send to the same page, only with different values. However, when you enter the if(shows the result) and then when it returns the page and tries the option of else if, doesn’t work. How do I fix this? As an example:

RequestDispatcher rd;
if (/*condicao*/) {
    arquivo.tarefa1 = 0;
    request.getSession().setAttribute("arquivo", arquivo);
    rd = request.getRequestDispatcher("resposta1.jsp");
}
else {
    arquivo.tarefa2 = 1;
    request.getSession().setAttribute("arquivo", arquivo);
    rd = request.getRequestDispatcher("resposta1.jsp");         
}
rd.forward(request, response);

In this case, I did not put the different values that I pass to the page. but the necessary I believe is this. When executed and enters the if and then go back to the page and enter the else it doesn’t work. request passes file, an object I created.

  • 2

    Friend, read your question carefully and think carefully about the amount of code you have given us so we can know what you are trying to do.

  • 1

    Your question is confused.

2 answers

2


You can put different attributes in the request and read them back in JSP:

if (/*condicao*/) {
    request.setAttribute("caminhoTomado", "if");
    rd = request.getRequestDispatcher("resposta1.jsp");
}
else {
    request.setAttribute("caminhoTomado", "else");
    rd = request.getRequestDispatcher("resposta1.jsp");         
}
rd.forward(request, response);
  • I already use request.setAttribute. I updated the code above.

  • You are always setting the same thing in both if and Else. Here in my answer I am setting different things.

  • yes, the problem that within the if and Else I need to change some values of the file object.

  • @Peace And why is that a problem? You can’t use one <c:if test="${requestScope.arquivo.tarefa1 == 0}">...</c:if> or something like that?

  • I don’t know if I understood what you said. but I need this to be in Servlet(java) and I need the conditions. As I said in the question, each button is where it enters the condition. And if the user goes back to page and click another option, is giving problem.

  • First, can’t or won’t touch the JSP? Second, this only happens when the user chooses option 1 the first time and option 2 the second time, correct? And if the user chooses option 2 for the first time and option 1 for the second time?

  • for other reasons need to be in Servlet. And no.. are 4 options, no matter what the order. The first option chosen works, the following do not.

  • @Peace If the first one works and the rest doesn’t, then your problem is probably with Session or the use of global variables on the server.

Show 3 more comments

0

insira o código aquiSee what you’re doing:

RequestDispatcher rd;
if (/*condicao*/) {
    arquivo.tarefa1 = 0;
    request.getSession().setAttribute("arquivo", arquivo);
    rd = request.getRequestDispatcher("resposta1.jsp");
}
else {
    arquivo.tarefa2 = 1;
    request.getSession().setAttribute("arquivo", arquivo);
    rd = request.getRequestDispatcher("resposta1.jsp");         
}
rd.forward(request, response);

Here,

request.getSession().setAttribute("arquivo", arquivo);

You make it possible to use the "file" object by request in your jsp resposta1.jsp.

With respect to

arquivo.tarefa1 = 0;

arquivo.tarefa2 = 1;

obs.: task1 and task2 are public attributes? public task1.....

Otherwise, it should be.setTarefa1(0) instead of.tarefa1 = 0;

You are indicating that the task1 attribute of the arquvo object has value 1 and did not give value to task2.

When you get to your jsp, you won’t know where your parole came from. Whether in if or Else. Unless you check in jsp between task1 and task2 who is not empty.

Got it?

Browser other questions tagged

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