【问题标题】:Result redirection not working with jsp结果重定向不适用于jsp
【发布时间】:2014-02-15 12:26:32
【问题描述】:

我在我的 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 中添加任何内容?

【问题讨论】:

  • 好吧,您的 JSP 主体中没有任何内容。那么它为什么要显示任何东西呢?此外,您正在做的是转发(这很好),而不是重定向,这是完全不同的事情。

标签: java jsp servlets


【解决方案1】:

在您的 servlet 中:

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

在您的 JSP 中:

<pre>The data from servlet: ${result}</pre>

【讨论】:

    【解决方案2】:

    您的 servlet 必须是:

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
                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);        
                RequestDispatcher view = request.getRequestDispatcher();
                view.forward(request, response);
         }
    

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 2017-08-28
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2010-12-08
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多