2016-11-29 39 views
-1

我嘗試在Servlet中將文本保存爲屬性並將其轉發到HTML頁面。然後需要在Html頁面主體中顯示它。 幫我發送數據作爲對HTML的響應,然後幫助顯示HTML頁面中的值。 在JSP中工作正常。但我需要將響應發送到html頁面並顯示它。如何從Servlet中獲取參數到Html頁面?

Here i am using Request dispatcher for send the request and response to html page. 
but i am not clear with how to display it in html.Help me tp solve. 

thanks 

//NewServlet.java 

public class NewServlet extends HttpServlet { 
protected void processRequest(HttpServletRequest request,HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    request.setAttribute("Cricket", "Sachin"); 
    RequestDispatcher rd = request.getRequestDispatcher("index.html"); 
    rd.forward(request, response); 
} 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
    } 
} 

//index.html

enter image description here

+0

'index.html'不是jsp –

+0

當你有jsp的時候使用JSTL –

+0

我能想到的唯一方法就是預處理html,並用你想顯示的任何東西替換「TODO寫內容」。 –

回答

-1

如果index.html文件在另一臺服務器上,你並不需要,因爲你需要做的另一個請求使用要求的屬性,而servlet API不提供你所需要的。

例如,您可以通過使用HttpURLConnection來獲取文件的內容。然後,您需要處理該內容以插入數據,然後將結果寫入servet響應。

相關問題