摘錄自:http://www.360doc.com/content/11/0316/13/5790498_101627263.shtml
把某一對象置於session范圍內,並在JSP頁面中提取session中放進去的對象的過程
Servlet代碼:
List<String> list=new ArrayList<String>();
list.add("王利虎");
HttpSession session=request.getSession();
//把某一對象置於session范圍內
session.setAttribute("LIST",list);
//提取session中放進去的對象
JSP代碼:
List list=(List)session.getAttribute("LIST");
這樣就OK了,實際開發中會碰到在JS代碼中獲取到session范圍中的某個值,遇到這樣的問題該怎么解決呢?
其實很簡單,為了和上面形成對比,我將此代碼留給大家以做參考:
Servlet代碼:
String myName="王利虎";
HttpSession session=request.getSession();
session.setAttribute("MYNAME",myName);
JS代碼:
<script language="JavaScript">
function getMyName(){
var myName="<%=session.getAttribute("MYNAME")%>";
alert(myName);
}