如何从Servlet到Html页面获取参数?

时间:2016-11-29 06:23:09

标签: java html jsp servlets requestdispatcher

我尝试在servlet中保存Text as Attribute并将其转发到HTML页面。然后需要在Html页面体中显示它。     帮助我发送数据作为对HTML的响应。然后帮助显示HTML页面中的值。     在JSP工作正常。但我需要将响应发送到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

1 个答案:

答案 0 :(得分:-1)

如果文件index.html在另一台服务器上,则不需要使用请求的属性,因为您需要执行另一个请求,而servlet API不能提供您所需的内容。

例如,您可以使用HttpURLConnection获取文件的内容。然后,您需要处理该内容以插入数据,然后将结果写入servet响应。