繁体   English   中英

从Servlet到JSP获取数据而无需转发?

[英]Get data from servlet to JSP without forward?

我需要从Servlet到JSP中标记的双精度值。 我的doGet()正在使用ArrayList中的值发送回格式化的HTML表,因此在完成这项工作后,我决定解决这一部分。

Servlet的:

//获取我需要的表的代码

    //Send back the result, this all works good
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(returnAsHTML.toString());

我添加尝试获得双倍价值的产品

    //Send back the result

    double test = 20;
    request.setAttribute("Test",test);

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(returnAsHTML.toString());

    getServletContext().getRequestDispatcher("index.jsp").forward(request,response);

在JSP中:

  <!-- This variable is unresolved -->
  <small>Test : ${Test}</small>

转发似乎使整个聚会崩溃。 我是JSP的新手,我敢肯定我缺少一些小东西。 我需要将response.getWriter()内容保留在其中,它会获得很多我需要的信息。 现在,我只是也不知道如何获取我需要的double值,因为它们将显示在页面的整个不同部分。

您不能将输出写入servletOutputStream 并同时 重定向

您对浏览器有什么期望:显示内容或导航到另一个页面? 如果是第一个,请不要重定向。 如果第二个,则不显示HTML内容。

尝试使用httpsession。 使用session.setAttribiute()封装任何对象。 然后使用sendRedirect()方法转发它。

可能是这种

HttpSession session=request.getSession();
double test = 20;
session.setAttribute("Test",test);
response.sendRedirect("Index.jsp");

在JSP页面上

String s=(String)session.getAttribite("Test");
double d=Double.parseDouble(s);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM