一尘不染

结果重定向不适用于jsp

jsp

我在servlet中收到ping结果,试图将其重定向到另一个jsp文件。

将打开用于输出的jsp文件,但其中没有任何显示。

这是我的servlet主要代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
        String ip = request.getParameter("ip"); 
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    //  out.println("The ip address is:"+ip+"\n");
        String result = pingTest(ip);
        out.println(result);
        String redirect = "Output.jsp";
        RequestDispatcher view = request.getRequestDispatcher(redirect);//Is it good approach to redirect request in ajax based servlet?
        view.forward(request, response);
        }

这是我的output.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ping Check Result</title>
</head>
<body>
</body>
</html

我是否需要在output.jsp中添加任何内容?


阅读 175

收藏
2020-06-10

共1个答案

一尘不染

在您的servlet中:

request.setAttribute("result", result);
request.getRequestDispatcher("/WEB-INF/Output.jsp").forward(request, response);

在您的JSP中:

<pre>The data from servlet: ${result}</pre>
2020-06-10