1

I have a webservlet, "Home.jsp" will get loaded on opening the web application, so it will call doGet in the servlet and it will forward it to dashboard jsp page

@WebServlet("/Home.jsp")
public class HomeController extends HttpServlet {...
....

request.getRequestDispatcher("central_dashboard.jsp").forward(request,response);

So, in the dashboard page, i have a javascript funtion, where i call another servlet page "Process.do", the issue is the get method is called, and it is not forwarding the page to "results.jsp" on getRequestDispatcher.

//Calling the webservlet from the jsp page

function res_call(d, i) {
    $.ajax({  
        type: "GET",  
        url: "Process.do",                             
      });

}

Process.do

request.getRequestDispatcher("results.jsp").forward(request, response); 

Note: All the jsp files are under webcontent folder

I have read some posts here, tried to return the function in the webservlet, but of no use. I am not sure where the issue is.


Posted the code,

   @WebServlet("/Home.jsp")
    public class HomeController extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                t

ry {
                doProcessRequest(request, response);
            } catch (SQLException | JSONException | ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                doProcessRequest(request, response);
            } catch (SQLException | JSONException | ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    protected void doProcessRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("name",val);   
        request.getRequestDispatcher("dashboard.jsp").forward(request,response);
    }
}

    @WebServlet("/Process.do")
    public class ProcessController extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see 

HttpServlet#HttpServlet()
     */
    public ProcessController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            doProcessRequest(request, response);
        } catch (SQLException | JSONException | NumberFormatException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            doProcessRequest(request, response);
        } catch (SQLException | JSONException | NumberFormatException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    protected void doProcessRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("name",val);   
        request.getRequestDispatcher("results.jsp").forward(request, response);
        return;

    }
}

dashboard.jsp - from where the central.jsp gets included, where i will call the js function, which gets called, and Process.do - doGet methods gets executed, but the results.jsp page is not getting forwarded/opened.

<!DOCTYPE html>
<meta charset="utf-8">
    <head>
    <title></title>
    </head>
    <body>
    <div class="centraltest">
     <%@ include file="central.jsp"%>
    </div>
    </body>
Vignesh Paramasivam
  • 2,360
  • 5
  • 26
  • 57

1 Answers1

0

1) The thing is that try to understand the fact that when you send http request from ajax, it means that you are sending the request in separate thread and not in the main thread (the page itself from where you are sending the request). So redirection at the servlet will not reflect at the client end.

In order to achieve this, send back the URL to which you want to redirect as a response to request and on success method of ajax simply use java script window.location(URL)

Inside servlet:

JSONObject jobj = new JSONObject();
jobj.put("url","central_dashboard.jsp");
response.getWriter().write(jobj.toString());

At client end:

function res_call(d, i) {
  $.ajax({
      url: "Process",
      type: "GET",
      dataType : 'json',
      cache: false,
      data: { 'url': 'value' }, 
      success: function(data){
          window.location = data.url;
                }
 });
}

Note: when you call a servlet via AJAX, you stay on the same page by definition, regardless of the headers sent by the server.

If you want to change the page, you must do it with javascript, in the success handler function for the $.ajax(..) call.

You can read the Location response header sent by the servlet response and set the window.location.href to that value, check other options for redirection.

2) you can use the below code in Process servlet's doGet method as an alternative.

response.getOutputStream().print("<script> window.location.href='central_dashboard.jsp'; </script>");   
//make sure this is the last line of your program control flow, because from here
//the response will be sent to central_dashboard.jsp

Instead of the below code in Process servlet:

request.getRequestDispatcher("central_dashboard.jsp").forward(request,response);
Community
  • 1
  • 1
Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40
  • Tried that now, no forwards to the page, and no error logs to chase for – Vignesh Paramasivam Oct 02 '16 at 15:17
  • In Process.do just before request.getRequestDispatcher("results.jsp").forward(request, response); add a print statement and let me know whether it prints something. – Rohit Gaikwad Oct 02 '16 at 15:23
  • Yes its printing! I already tried and confirmed that the method is getting executed. – Vignesh Paramasivam Oct 02 '16 at 15:27
  • urlToRedirect to jobj.put("url",urlToRedirect ); is this it? i have added this in the /Home servlet, and above the getDispatcher, and no luck – Vignesh Paramasivam Oct 02 '16 at 17:00
  • Check the updated JS code and note. You need to debug the JS and check what is set in the "data" field of ajax call and does the handler goes inside success function, later is the value of window.location is properly or not. – Rohit Gaikwad Oct 02 '16 at 17:37
  • Both doesn't work for me, not sure where i am going wrong. The doGet method is getting called, and its not loading the new jsp page – Vignesh Paramasivam Oct 03 '16 at 09:51