RequestDispatcher 未在 java servlet 中重定向到 JSP

RequestDispatcher not redirecting in java servlet to JSP

我正在尝试从我的 servlet 重定向到字符串值为空的另一个页面。当我 运行 代码时,它停留在同一页面上,而不是重定向到我的错误页面。这是我的代码:

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); 
}

我没有收到任何错误,打印出来的结果为 null 或 value,具体取决于是否存在值。

您必须对代码进行以下更改。请求调度程序不适用于 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();
 }
}

或者您可以 Ajax 成功。这样你就不需要为我的更改代码。 如果你想用 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
        }
    }
});

当然你的servlet必须改成下面这样。

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();
}

您在 UI 中看不到任何内容,因为您的 error.jsp 文件中没有任何内容(没有文本内容)。

您需要在error.jsp页面添加如下文字:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
</head>
<body>
   <p>Error has occurred!!!</p>
</body>

当你的请求被转发到一个jsp/servlet/html并不意味着你的浏览器url会改变,只是你的请求会被转发到另一个资源生成一个将发回给您的回复。 URL 将保持不变。