留言板

How to use session?

Toshiro Mifune,修改在8 年前。

How to use session?

Regular Member 帖子: 238 加入日期: 14-12-11 最近的帖子
Good day,

At first : sorry if this question doesn't belong here. Thanks. In this case, please ignore it. Or tell me and I delete this question. Thanks.

I am trying to create a session and store/set into variable some value, but everything I tried was wrong and always shows some error.
I tried this code :

try{
    request.getSession().setAttribute("lastname","Kurosawa");
    request.getSession().getAttribute("lastname");
}catch(Exception ex2){
    message0 = "ERROR : " + ex2;
 }

ERROR : java.lang.NullPointerException

Summ : how to use session in JAVA? Thank you.
thumbnail
Juan Gonzalez,修改在8 年前。

RE: How to use session?

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
If you are using JSF, you can use @SessionScoped beans:

http://docs.oracle.com/javaee/6/tutorial/doc/girch.html

Then, the bean attributes will be actually stored in session.
Toshiro Mifune,修改在8 年前。

RE: How to use session?

Regular Member 帖子: 238 加入日期: 14-12-11 最近的帖子
What do you say on this? It is usefull?

I am just trying examples ...

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("asd", "123");			  
String nn = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("asd").toString();

... it wors, but no idea if it is that session I know ...
... because as I think to myself ... if I set some session in javascript it have to be read in java, haven't it? ... because it is session and it depends from which language I come to this session.

OR IF I AM WRONG TELL ME, PLEASE :-)
thumbnail
Juan Gonzalez,修改在8 年前。

RE: How to use session?

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
Toshiro Mifune:


FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("asd", "123");			  
String nn = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("asd").toString();

... it wors, but no idea if it is that session I know ...


Google and JSF API is your friend emoticon
http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/api/javax/faces/context/ExternalContext.html

Toshiro Mifune:

if I set some session in javascript it have to be read in java, haven't it?


I don't understand this. Do you want to set session attributes throught javascript?
Toshiro Mifune,修改在8 年前。

RE: How to use session?

Regular Member 帖子: 238 加入日期: 14-12-11 最近的帖子
:-) I know my english isn't perfect ...
and yes, I want to store session in javascript and read it in JAVA
thumbnail
Juan Gonzalez,修改在8 年前。

RE: How to use session?

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
Well, this is not the right place for general JSF questions, but I will try to help.

If you want to store something in session from javascript probably you should execute an action or actionListener and set the value there.

For creating a command from javascript I would use Primefaces remotecommand.
Toshiro Mifune,修改在8 年前。

RE: How to use session?

Regular Member 帖子: 238 加入日期: 14-12-11 最近的帖子
Sorry I forgot to mention in my previous messages ... forgot about javascript ... I just know how to set and get value from session variable ... for example .. if I set session var in one portlet it should appear in another portlet ... again sorry for wrong formulated question :-)
thumbnail
Juan Gonzalez,修改在8 年前。

RE: How to use session?

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
If you just simply want to "send" some data to other portlets, you can use InterPortlet Communication (IPC). You have instructions for that here:

https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/communicating-between-jsf-portlets-using-ipc

If not, there is a mechanism to share session attributes between portlets (it's like IPC but simpler):

https://www.liferay.com/community/wiki/-/wiki/Main/Portlet+to+Portlet+Communication#section-Portlet+to+Portlet+Communication-1.+Portlet++Session

Hope it helps.
Toshiro Mifune,修改在8 年前。

RE: How to use session?

Regular Member 帖子: 238 加入日期: 14-12-11 最近的帖子
OK so now I am trying to use portlet session ... http://www.liferay.com/community/wiki/-/wiki/Main/Portlet+to+Portlet+Communication
When I use this code ...

PortletSession ses = renderRequest.getPortletSession();
ses.setAttribute("sessionValue", "testingValue" ,PortletSession.APPLICATION_SCOPE)

... it doesn't know 'renderRequest' ... I use JSF now
thumbnail
Juan Gonzalez,修改在8 年前。

RE: How to use session?

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
Hi Toshiro,

you can use this kind of code:


FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
PortletSession portletSession = portletRequest.getPortletSession();