0

Before posting this question I cross referenced previous questions on this and other communities blog but my problem remains.

Problem

I am not able to redirect my servlet to a new jsp from the doPost() method in the servlet. I called the doPost method from the javascript file as shown below in the code snippets. The control comes to the doPost() method which I checked through System.out.println statements but it never dispatches to the helloWorld.jsp I am using eclipse/and Tomcat 9.0.

My Servlet code:

 @WebServlet(
        name = "OpenSourceMapCtrl",
        description = "Open Source Map Main Controller",
        urlPatterns = {"/callMasterDateHandler"}
    )
public class MainAppCtrl extends HttpServlet{


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            try {
RequestDispatcher dispatcher =request.getRequestDispatcher("/helloWorld.jsp");
        dispatcher.forward(request, response);
            } catch(Exception e){
                e.printStackTrace();
            }
        }   
    }

enter image description here helloWorldJs.js

$.post("callHelloWorldHandler", function(data) {
                console.info('@#@ data = ',data);
});

The firebug console shows 200 Ok for the page "hello world.jsp". But the page does not render.

raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • @BalusC Hi, I couldn't come across https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax this link before so had to post this question. I went through the link in detail and I guess it is the most detailed explanation to the problem. Would it be a nice idea to give the link as an answer to this question and I can select that as the correct answer. The mechanism that servlet can't forward/redirect the call to a jsp page when invoked from a ajax/post/get callback was the learning for me and I tried to retain that learning through an answer and subsequent links I came across. – raikumardipak Jun 16 '17 at 06:12

2 Answers2

0

response.sendRedirect will not work for two reasons: (1) You can't redirect as POST. Redirects are always GET. (2) You cannot redirect to a file in WEB-INF because the browser has no direct access to it. So you have to use forward.

The jsp is under WEB-INF, so you have to specify that in the dispatcher: request.getRequestDispatcher("/WEB-INF/helloWorld.jsp").forward(request, response);

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • I made the above changes but my jsp page still doesn't render. In the above sample code before, I changed to a get call and tried the redirect it still didn't work. I guess I am all over the place on this one. Tumbleweed. – raikumardipak Jun 14 '17 at 04:51
0

Thanks to this post

How to redirect to a new page in jquery callback function

which finally helped me to zero down on the root cause for the problem.

In my javascript file I am trying to call the servlet from a jquery callback. Though it is not exactly an ajax call but $.get or $.post still behaves as an ajax call, i.e. to say more exactly as a callback to which the control must return. So, when you do a
RequestDispatcher dispatcher =request.getRequestDispatcher("helloWorld.jsp"); dispatcher.forward(request, response); or a response.sendRedirect("helloWorld.jsp"); The servlet does render the “helloWorld.jsp” but not directly to the browser but to the awaiting “callback” function in my javascript file from where it was invoked. Had I done a form submit action through the form “action” event in the jsp then the servlet could have rendered the “helloWorld.jsp” directly to the browser.

You can find a very detailed and technical explanation to the entire use case above in this link below:

How to use Servlets and Ajax?

raikumardipak
  • 1,461
  • 2
  • 29
  • 49