如何从Java文件到JSP页面获取值?

时间:2014-12-31 18:28:08

标签: java jsp

在我的代码中,我将值存储在字符串数组中,并希望在我的JSP页面中显示这些数组值。我试图使用session和setArrtibute属性,但它不起作用。

if (button.equals("Finish")) {
    pname[n] = request.getParameter("name");
    pemail[n] = request.getParameter("email");

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/invite2.jsp");
    dispatcher.forward(request, response);
    n++;
}

invite2.jsp

<form method="get" name="create" action="MainController">

    <h2>Invite participants</h2>
    <h3> you invited </h3>

    <ul><li> </li>
        <li></li>
        <li> </li>
    </ul>

    <input type="submit" name="button" value="Cancel" style="height:30px; width:120px">
    </input>

    <input type="submit" name="button" value="Confirm" style="height:30px; width:120px">
    </input>
</form>

1 个答案:

答案 0 :(得分:1)

您可以使用

<c:out value="${sessionScope.att1}"/>
<c:out value="${requestScope.att2}"/>

访问(打印)会话或请求属性的内容。您必须在forward()之前设置它们。 (如果它是纯粹的请求特定值,则不使用会话):

request.getSession().setAttribute("att1", "hello");
request.setAttribute("att2", "world");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/invite2.jsp");
dispatcher.forward(request, response);

This turorial详细描述了整个过程。包括JSP端以格式化表的bean(表示Books)。

相关问题