[英]Forward from JSP to another JSP without using a servlet between them?
[英]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.