【问题标题】:How to get the parameter from Servlet to Html page? [duplicate]如何从 Servlet 获取参数到 Html 页面? [复制]
【发布时间】:2016-11-29 06:23:08
【问题描述】:

我尝试将文本保存为 servlet 中的属性并将其转发到 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

【问题讨论】:

  • index.html 不是jsp
  • 当你有 jsp 时使用 JSTL
  • 我能想到的唯一方法是预处理 html,并将“TODO 写入内容”替换为您想要显示的任何内容。
  • 但是使用 JSP 会容易得多
  • 你为什么需要 html?

标签: java html jsp servlets requestdispatcher


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多