0

I am trying to print the output of a java file to a jsp file.

for(String x : list)
{
    try {
        output 1
    }
    catch {
        output 2
    }
}

The output 1 should go to x.jsp file The output 2 should go to y.jsp file

Where do I give response.Redirect(x.jsp) and response.Redirect(y.jsp)?

If I give each statements within each block/try and catch) it will throw error:

cannot redirect after committing

Any help is much appreciated. Let me know if you don't understand my question correctly.

Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
user3622196
  • 71
  • 3
  • 16
  • Don't print out a bunch of stuff before trying to redirect. If you're going to redirect, make it simple. And make sure to do `return;` right after response.sendRedirect too. – developerwjk May 14 '14 at 23:56

2 Answers2

1

I was able to do it. selection of radio buttons

if(radio value==(value of 1st radio button))
{
for(String x : list)
{
   try {
    output 1
      RequestDispatcher dispatcher =request.getRequestDispatcher("1st.jsp");
      dispatcher.forward(request, response);
      return;
}
catch {
    output 2
      RequestDispatcher dispatcher =request.getRequestDispatcher("2nd.jsp");
      dispatcher.forward(request, response);
      return;
  }
  }
  }
 else{

 for(String x : list)
  {
  try {
    output 1
      RequestDispatcher dispatcher =request.getRequestDispatcher("1st.jsp");
      dispatcher.forward(request, response);
      return;
}
catch {
    output 2
      RequestDispatcher dispatcher =request.getRequestDispatcher("2nd.jsp");
      dispatcher.forward(request, response);
      return;
}
 }
}

based on this it will go to one of the jsp pages depending upon the selection of radio buttons.

I hope this will help people in the future too.

thanks all for helping me out.

user3622196
  • 71
  • 3
  • 16
0

response.sendRedirect() method creates a new request every time , why dont you use the RequestDispatcher and add a return statement to it .

see this RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

so you may rewrite your code as ,

for(String x : list)
{
    try {
        output 1
          RequestDispatcher dispatcher =request.getRequestDispatcher("1st.jsp");
          dispatcher.forward(request, response);
          return;
    }
    catch {
        output 2
          RequestDispatcher dispatcher =request.getRequestDispatcher("2nd.jsp");
          dispatcher.forward(request, response);
          return;
    }
}

Hope this helps!

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Thank you so much for your help. I guess this should work. I need to create a radio button in jsp and 1st option should go to try option and print 1st.jsp and 2nd option should go to catch and print 2nd.jsp.Should I create a if statement for the radio button and go through the try catch blocks. – user3622196 May 15 '14 at 17:41