【问题标题】:retrieving the attribute value from the request.getSession从 request.getSession 中检索属性值
【发布时间】:2018-10-06 03:05:06
【问题描述】:

如果我做错了,请纠正我。

我正在使用下面的代码在 jsp 页面中显示。可以使用下面的代码 sn -p 来显示 companyName 值吗?

        <% String compName = (String)request.getSession().getAttribute("companyName"); %>

//    inside the form     
    <input type="hidden" name="companyName" value="<c:out value="${compName}" />" />
            <script>
                $('#myLink').on('click', function() { $('#apForm').submit(); });
            </script>

【问题讨论】:

    标签: java jsp session servlets request


    【解决方案1】:

    对于你的问题:答案是NO

    原因是compName的作用域是page,但是如果通过${compName}访问,则必须先设置值

    访问compName有两种方式:

    A.使用java代码获取

    <input type="hidden" name="companyName" value="<%=compName%>" />
    

    B.首先通过request设置属性值,然后照着做就可以了

    <% 
      String compName = (String)request.getSession().getAttribute("companyName");
      request.setAttribute("compName",compName);
     %>
    <input type="hidden" name="companyName" value="<c:out value="${compName}" />" />
    

    【讨论】:

    • 我想在隐藏参数中发送“compName”的值。我该怎么做?
    • 您的 scriptlet 代码是多余的。如果会话属性已经设置,OP 可以只使用 ${compName}。
    • @JonathanLaliberte 因为会话变量被称为companyName 而不是compName
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2021-06-03
    • 2013-09-03
    • 2013-05-22
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多