在有的時候我們可以直接使用application。getAttribute("username"),這里是:用戶名:<%=((User)application.getAttribute("myuser")).getUsername() %>,這是出于什么原因呢?在類似問題上確實很薄弱,希望有懂的同學老師幫忙解說一下,謝謝啦??!
在有的時候我們可以直接使用application。getAttribute("username"),這里是:用戶名:<%=((User)application.getAttribute("myuser")).getUsername() %>,這是出于什么原因呢?
2015-10-14
application.getAttribute("username") 返回的是String
application.getAttribute("Users") 返回的是class
2015-09-15
是因為這里的application中"myuser"對應的value值是一個類的對象。像是這樣。
我沒有看過這個視頻,不知道具體是怎樣的,但我這樣寫你應該能看懂吧。
User u = new User("Li",21);//有一個User類,屬性有username,age。這里的參數,也可以從網頁表單傳過來
request.getSession().getServletContext().setAttribute("myuser",u);
那么在jsp中我要拿到這個用戶的username怎么拿?
<%=((User)application.getAttribute("myuser")) %>這樣拿到的是u這個對象,再調用u.getUsername()不就可以拿到username了嗎。User這里類里面肯定寫了getUsername()這個方法的。
那么,連起來不就是<%=((User)application.getAttribute("myuser")).getUsername() %>
-------------
而application.getAttribute("username")這樣用的時候,是你在application中存了("username",具體的名字);
所以,主要是因為一個存的就是你要的數據(username),而另一個是存了包含這個數據(username)的對象(u),所以要在拿到這個對象之后在調用它自己的方法(getUsername()),去拿到數據。
2015-09-14
jsp內置對象