2016-11-29 14 views
-1

サーブレットのテキストを属性として保存し、HTMLページに転送しようとしています。そのため、HTMLページの本文に表示する必要があります。 HTMLへの応答としてデータを送信するのを手伝ってください。HTMLページの値を表示するのに役立ちます。 JSPでうまくいきました。しかし、私はHTMLページに応答を送ってそれを表示する必要があります。サーブレットから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

jspを使用している場合、 'index.html'はjsp –

+0

ではありません。JSTL –

+0

私はHTMLを前処理し、" TODO write content "を何でも表示したいものに置き換えることができます。 –

答えて

-1

index.htmlファイルを別のサーバー上にある場合、あなたは別の要求を行う必要があるため、要求の属性を使用する必要はありません、サーブレットAPIは必要なものを提供しません。

たとえば、HttpURLConnectionを使用してファイルのコンテンツを取得できます。次に、その内容を処理してデータを挿入し、結果をservet応答に書き込む必要があります。

関連する問題