我有一个webservlet," Home.jsp"将在打开Web应用程序时加载,因此它将在servlet中调用doGet并将其转发到仪表板jsp页面
@WebServlet("/Home.jsp")
public class HomeController extends HttpServlet {...
....
request.getRequestDispatcher("central_dashboard.jsp").forward(request,response);
因此,在仪表板页面中,我有一个javascript函数,我调用另一个servlet页面" Process.do",问题是调用了get方法,并且它没有将页面转发到" results.jsp"在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);
注意:所有jsp文件都在webcontent文件夹
下我在这里看过一些帖子,试图在webservlet中返回该函数,但没有用。我不确定问题出在哪里。
发布代码,
@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 - 从其中包含central.jsp,我将调用js函数,调用它,并执行Process.do - doGet方法,但result.jsp页面没有被转发/打开
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title></title>
</head>
<body>
<div class="centraltest">
<%@ include file="central.jsp"%>
</div>
</body>
答案 0 :(得分:0)
1)问题是尝试理解当您从ajax发送http请求时,这意味着您是在单独的线程中而不是在主线程中发送请求(页面本身就是你发送请求的地方)。因此,servlet上的重定向不会反映在客户端。
为了实现这一点,请将要重定向的URL作为对请求的响应发送回来,并在ajax的成功方法上使用java脚本window.location(URL)
内部servlet :
function res_call(d, i) {
$.ajax({
url: "Process",
type: "GET",
dataType : 'json',
cache: false,
data: { 'url': 'value' },
success: function(data){
window.location = data.url;
}
});
}
在客户端:
window.location.href
注意:当您通过AJAX调用servlet时,无论服务器发送的标头如何,您都可以按照定义停留在同一页面上。
如果您想更改页面,则必须使用javascript,在$ .ajax(..)调用的成功处理函数中执行此操作。
您可以阅读servlet响应发送的位置响应标头,并将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
设置为该值,检查其他options是否有重定向。
2)您可以在处理 servlet的doGet方法中使用以下代码作为替代方法。
request.getRequestDispatcher("central_dashboard.jsp").forward(request,response);
而不是Process servlet中的以下代码:
{"hour": "00", "month": "07", "second": "00", "year": "1970", "timezone": "-00:00", "day": "12", "minute": "00"}
{"hour": "00", "month": "07", "second": "01", "year": "1970", "timezone": "-00:00", "day": "12", "minute": "00"}
{"hour": "00", "month": "07", "second": "02", "year": "1970", "timezone": "-00:00", "day": "12", "minute": "00"}
{"hour": "00", "month": "07", "second": "03", "year": "1970", "timezone": "-00:00", "day": "12", "minute": "00"}
{"hour": "00", "month": "07", "second": "04", "year": "1970", "timezone": "-00:00", "day": "12", "minute": "00"}