gpt4 book ai didi

java - RequestDispatcher 未将 java servlet 中的重定向到 JSP

转载 作者:行者123 更新时间:2023-12-01 09:25:22 26 4
gpt4 key购买 nike

我正在尝试从我的 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 或值,具体取决于是否有值。

最佳答案

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

关于java - RequestDispatcher 未将 java servlet 中的重定向到 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com