简体   繁体   English

从页面到页面JSP检索映射属性

[英]Retrieve map attribute from page to page JSP

i am trying to get retrieve attributes which are being put into a map. 我试图获取正在放入地图的检索属性。 However i keep getting null when i try to debug it by putting it in an alert. 但是,当我尝试通过将其置于警报中来调试它时,我一直得到null。 Any help will be appreciated, thank you! 任何帮助将不胜感激,谢谢!

First jsp 第一个jsp

<%
   //Map newSurvey = new LinkedHashMap();
   Map newSurvey = new HashMap();
   newSurvey.put("description", request.getParameter("description"));
   newSurvey.put("startDate", request.getParameter("start_datetime"));
   newSurvey.put("endDate", request.getParameter("end_datetime"));
   newSurvey.put("maxParticipant", request.getParameter("max_participant"));
   newSurvey.put("minAge", request.getParameter("min_age"));
   newSurvey.put("maxAge", request.getParameter("max_age"));
   newSurvey.put("preSurveyText",  request.getParameter("pre_survey_text"));
   request.setAttribute("myMap", newSurvey);
%>
window.location = 'Survey_Questions.jsp';

Second jsp (to retrieve) i used a javascript to see if i am able to retrieve it 第二个jsp(用于检索),我使用JavaScript查看是否能够检索它

function testGet(){
        <%
        Map myMap = (Map)request.getParameter("myMap");
        String description = (String) myMap.get("description");
        %>
        alert(<%=description%>
    }

On first page you are using: request.setAttribute("myMap", newSurvey); 在第一页上,您正在使用: request.setAttribute("myMap", newSurvey); .

So you must have to use request.getAttribute("myMap"); 因此,您必须使用request.getAttribute("myMap"); on second page. 在第二页上。

window.location = 'Survey_Questions.jsp';

Will just redirect to a page and it will not pass your request object to other documents. 只会重定向到页面,而不会将您的request对象传递给其他文档。

Instead you can use below lines of code: 相反,您可以使用以下代码行:

RequestDispatcher rd = request.getRequestDispatcher("Survey_Questions.jsp");
rd.forward(request, response);

RequestDispatcher helps you to forward your request to other pages. RequestDispatcher可帮助您将request转发到其他页面。

Also, use getAttribute instead of getParameter as you have used setAttribute to set myMap on First.jsp. 另外,使用getAttribute代替getParameter因为您已经使用setAttributemyMap上设置myMap。

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

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