从Servlet到JSP获取数据而无需转发? [英] Get data from servlet to JSP without forward?
问题描述
我需要从servlet到我的JSP中的标记的双精度值.我的doGet()正在使用ArrayList中的值发送回格式化的HTML表,因此在完成这项工作后,我决定解决这一部分.
I need a double value from my servlet to markup in my JSP. My doGet() is sending back formatted HTML tables with values from an ArrayList, so after I got that working I decided to tackle this part.
Servlet:
//获取我需要的表的代码
//Code getting the tables I need
//Send back the result, this all works good
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(returnAsHTML.toString());
我添加尝试获得双精度值的东西
What I added to try and get the double value
//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值,因为它们将显示在页面的整个不同部分.
The forwarding seems to crash the whole party. I am new to JSP, I'm sure I am missing something small. I need to keep the response.getWriter() stuff there, it gets a lot of the information I need. Now I just don't know how to get my double values I need as well, because they will be displayed on a whole different part of the page.
推荐答案
您不能将输出同时写入servletOutputStream 和 重定向时间.
您对浏览器有什么期望:显示内容或导航到另一个页面?如果是第一个,请不要重定向.如果第二个,则不显示HTML内容.
What do you expect from the browser: to display content or to navigate to another page? If the first, don't redirect. If the second, don't display HTML content.
这篇关于从Servlet到JSP获取数据而无需转发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!