javajspservletsrequestdispatcher

RequestDispatcher not redirecting in java servlet to JSP


I'm trying to redirect to another page from my servlet where string value is null. When I run the code it stays on the same page instead of redirecting to my error page. Here is my code:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
  response.setContentType("text/html");
  String emp = request.getParameter("emp")!=null ? request.getParameter("emp"): "";
  MCenter mCenter = new MCenter();
  mCenter = mCenterDAO.getMCenterPocByEmp(emp);
  mCenter = mCenterDAO.getMCenterByObject(mCenter);

  PrintWriter pw = response.getWriter();
  String mPocName = mCenter.getMCenterPocName();
  String mCenter = mCenter.getMCenterName();
     if(mPocName == null || mCenter == null) {
        request.getRequestDispacher("error.jsp").forward(request, response);
     System.out.println("Null or not name " + mPocName + "center " + mCenter);
     }
   String json = getMCenterPoc(emp);
   pw.print(json);
   pw.close();
}

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

I get no error, and the print out give me null or value depending on whether or not there is value or not.


Solution

  • You must do following changes in your code. Request Dispatcher doesn't work with Ajax post.

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
     String emp = request.getParameter("emp") != null ? request.getParameter("emp") : "";
     MCenter mCenter = new MCenter();
     mCenter = mCenterDAO.getMCenterPocByEmp(emp);
     mCenter = mCenterDAO.getMCenterByObject(mCenter);
     String mPocName = mCenter.getMCenterPocName();
     String mCenter = mCenter.getMCenterName();
     if (mPocName == null || mCenter == null) {
      request.sendRedirect("error.jsp");
      System.out.println("Null or not name " + mPocName + "center " + mCenter);
     } else {
      response.setContentType("text/html");
      PrintWriter pw = response.getWriter();
      String json = getMCenterPoc(emp);
      pw.print(json);
      pw.close();
     }
    }
    

    Or you can do with Ajax success. In this way you don't need to change code to mine. If you want to do with Ajax.

    var emp = $('#emp').val();
    $.ajax({
        type: "POST",
        url: "yourServletName",
        data: 'emp='+emp+'',
        success: function(response) {
            if(response='error'){
                  window.location.href='error.jsp';
            }else{
                  //What you want
            }
        }
    });
    

    Of course your servlet must change to following.

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
     response.setContentType("text/html");
     PrintWriter pw = response.getWriter();
     String emp = request.getParameter("emp") != null ? request.getParameter("emp") : "";
     MCenter mCenter = new MCenter();
     mCenter = mCenterDAO.getMCenterPocByEmp(emp);
     mCenter = mCenterDAO.getMCenterByObject(mCenter);
     String mPocName = mCenter.getMCenterPocName();
     String mCenter = mCenter.getMCenterName();
     if (mPocName == null || mCenter == null) {
      String json = "error";
     } else {
      String json = getMCenterPoc(emp);
     }
     pw.print(json);
     pw.close();
    }