留言板
How to use session?
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 :
ERROR : java.lang.NullPointerException
Summ : how to use session in JAVA? Thank you.
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.
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.
http://docs.oracle.com/javaee/6/tutorial/doc/girch.html
Then, the bean attributes will be actually stored in session.
What do you say on this? It is usefull?
I am just trying examples ...
... 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 :-)
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 :-)
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
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?
:-) I know my english isn't perfect ...
and yes, I want to store session in javascript and read it in JAVA
and yes, I want to store session in javascript and read it in JAVA
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.
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.
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 :-)
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.
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.
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 ...
... it doesn't know 'renderRequest' ... I use JSF now
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
Hi Toshiro,
you can use this kind of code:
you can use this kind of code:
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
PortletSession portletSession = portletRequest.getPortletSession();