簡體   English   中英

使用JSTL設置會話變量並在servlet / controller類中訪問它

[英]Set session variable using JSTL and accessing it in servlet/controller class

如果我像這樣使用JSTL設置會話變量:

<c:set var="para" value="${CLIENT_LOGO}" scope="session"  />

那么如何在servlet / controller類中訪問變量“ para”?

我嘗試了以下代碼變體,但沒有一個起作用。

request.getAtrribute("para") 


request.getSession().getAtrribute("para") 

注意:我不是在尋找一種解決方案來在jsp中打印值,例如:

<c:out value="${sessionScope.para}" />

但是,我想知道是否有任何解決方案可以在Java類中獲得它。

您必須在servlet中執行以下代碼:

HttpSession session = request.getSession();
String para = session.getAttribute("para");

您可以使用JSTL設置session

<c:set var="para" value="valueHere" scope="session"  />

可以通過參考此代碼來解決此問題。

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <body>
    This JSP stores the ultimate answer in a session-scoped variable where
    the other JSPs in the web application can access it.
    <p />
    <c:set var="theUltimateAnswer" value="${41+1}" scope="session"  />

     Click <a href="displayAttributes.jsp">here</a> to view it.
  </body>
</html>

用於顯示價值

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <head>
    <title>Retrieval of attributes</title>
  </head>
  <body>
    The ultimate answer is <c:out value="${sessionScope.theUltimateAnswer}" /> <br/>
  </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM