Problems with else if in servlet

1

I'm messing with jsp. In my servlet I have if and else if and both send to the same page, only with different values. However, when you enter the if option (it shows the result) and then when you return the page and try the else if option, it does not work. How can I solve 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 case, I did not put the different values that I step to the page .. but the necessary I believe it to be that. When run and enter the if option and then return the page and enter the else option it does not work. The request passes file, an object I created.

    
asked by anonymous 15.01.2015 / 21:57

2 answers

2

You can put different attributes in the request and read them back in the 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);
    
15.01.2015 / 22:16
0

insira o código aqui See 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 per request in your jsp answer1.jsp.

With respect to

arquivo.tarefa1 = 0;

arquivo.tarefa2 = 1;

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

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

You are indicating that the task1 attribute of the file object has value 1 and has not given value to the task2 attribute.

When you get to your jsp, you will not know where the conditional came in. If not in the if or if in the else. At least check in jsp between task1 and task2 who is not empty.

Do you understand?

    
14.08.2015 / 14:18