While you're absolutely correct that the preferred method of getting to the
session attribute map is by implementing the SessionAware interface, calling
ServletActionContext.getContext().getSession() (as Nguyen did) will give you
the Map.

And you are also correct that if you actually need to get hold of the
HttpSession object, you can call
ServletActionContext.getRequest().getSession().
  (*Chris*)

On Sun, Nov 29, 2009 at 6:48 PM, KamHon Eng <kam...@gmail.com> wrote:

> to able to retrieve session object in Action class, there are 2 ways,
> 1st way is using ServletActionContext.getRequest().getSession()
> it will giving u a HttpSession object.
>
> 2nd way is, your Action class implements
> org.apache.struts2.interceptor.SessionAware. Please refer to JavaDoc.
>
> For your information, Action class should not call the ActionContext.
> The ActionContext normally used / called by Struts Interceptor.
>
>
>
>
> On Mon, Nov 30, 2009 at 9:57 AM, Nguyen Xuan Son <yama...@gmail.com>
> wrote:
> > dear all
> > I have the C0002_Home.jsp and sessionChk.jsp files. The C0002_Home.jsp's
> > source code is
> > <s:action name="SessionCheck"></s:action>
> >
> > the meaning of this source code is to check users session whenever they
> > enter the webpage
> > the struts.xml is structured as:
> > <action name="SessionCheck" class="com.baibai.action.SessionCheckAction">
> > <result name="input">sessionChk.jsp</result>
> > </action>
> >  <action name="C0002_Home" class="com.baibai.action.C0002_HomeAction">
> > <result name="input">C0002_Home.jsp</result>
> > </action>
> >
> > the detail of the SessionCheckAction.java file is
> > public class SessionCheckAction extends ActionSupport{
> > public String execute() throws Exception {
> > Map ses = ActionContext.getContext().getSession();
> >
> if((ses.get("context").toString()!="")&&(ses.get("role").toString()=="admin")){
> > // the user is loginned as an administrator
> > System.out.println("you have session");
> > } else {
> > System.out.println("you dont have session");
> > }
> > return SUCCESS;
> > }
> > }
> >
> > however when i tried to enter the C0002_Home.jsp without Login
> > the error appears
> > 2009/11/30 10:40:07
> > com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
> > 致命的: Could not execute action: /SessionCheck
> > java.lang.NullPointerException
> > at
> com.baibai.action.SessionCheckAction.execute(SessionCheckAction.java:10)
> > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> > at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> > at java.lang.reflect.Method.invoke(Unknown Source)
> >
> > this doesnt happen if I login into the system
> > could you tell me where did I went wrong
> >
> > --
> > =======================================================================
> > Ritsumeikan University, Asia JinZai Project
> > Master of Information Science
> > Nguyen Xuan Son
> >
> > Add       : Japan, Shiga-Ken, Kusatsu-Shi, Kasayama 3choume 1-18
> ShiteiHaimu
> > Rien, Room 103
> > Tel/Fax  : 81-(0)90-3976 2246
> > Email    : nr000...@ed.ritsumei.ac.jp
> > Mobile   : 81-(0)90-3976 2246          URL  : http://www.ritsumei.jp
> > =======================================================================
> >
>
>
>
> --
> Best Regard,
>
> Kam Hon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

Reply via email to