2014-02-15 62 views
0

我在我的servlet中得到了ping結果。我試圖將其重定向到另一個jsp文件。結果重定向不能用於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的補充?

+0

那麼,有沒有在你的JSP主體。那爲什麼它應該顯示任何東西?此外,你正在做的是轉發,(這很好),但不重定向,這是一個完全不同的東西。 –

回答

1

在你的servlet:

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

在你的JSP:

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

它工作。謝謝阿列克謝。 – Randeep

+0

不客氣。 –

0

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