0

I did one application, In that I should maintain a login user session, I created one login Interceptor to maintain this. I inherited for doing hibernate session maintains purpose. When I used Login Interceptor along with "hibernate-default" it's throw Nullpointer exception. here hibernate session is showing NULL. How to maintain hibernate session.

Below is my struts.xml

 <package name="helloworld" extends="hibernate-default">
    <interceptors>
        <interceptor name="mylogging" class="Demo.AuthenticationInterceptor"></interceptor>
        <interceptor-stack name="loggingStack">
            <interceptor-ref name="mylogging" />
            <interceptor-ref name="defaultStack" />                
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="loggingStack"></default-interceptor-ref>
    <action name="signin" method="checkLogin" class="Demo.LoginForm">
        <result name="input">/login.jsp</result>
        <result name="success" type="redirect">/list.jsp</result>
        <result name="failure">/error.jsp</result>
    </action>
    <action name="listUser" method="list" class="Demo.UserAction"> 
        <interceptor-ref name="basicStackHibernate"/>           
        <result name="success">/register1.jsp</result>
        <result name="login">/login.jsp</result>
    </action>
 </package>
</struts>     

I used for maintain session for every action but my session showing NULL.

My AuthenticationInterceptor.java

public class AuthenticationInterceptor implements Interceptor
{
  public String intercept(ActionInvocation ai) throws Exception 
 {
    System.out.println("inside the interceptor()......new");
    Map session1 = ai.getInvocationContext().getSession();
    String name = (String) session1.get("name");
    System.out.println("inside the session or loginaction=" + name);
    if ((session1.get("name") != null) || (session1.get("name") == null))
   {
        System.out.println("inside the session or loginaction ");
        return ai.invoke();
    } else {
        return "login";
    }
}
}

Exception is:

Session =null
java.lang.NullPointerException
at Demo.UserDAOImpl.listUser(UserDAOImpl.java:57)
at Demo.UserAction.list(UserAction.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
null
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  • [link1](http://stackoverflow.com/a/18502757/1031945) and [link2](http://stackoverflow.com/a/18663607/1031945) – Aniket Kulkarni Dec 19 '13 at 05:44
  • @Aniket thank u aniket. everything is working but except update while updating it's not updated and added another column with empty data. –  Dec 19 '13 at 06:38
  • update the question and show the exception/error that you are getting – Aniket Kulkarni Dec 19 '13 at 07:06
  • @Aniket At last I added Exeption –  Dec 19 '13 at 09:21
  • @Aniket Update also working. Now the problem is I given registration page separate before login. While registering new data it's stored into DB null values. If I do update from my jsp page it's updating successfully updating. What I will do now .? –  Dec 19 '13 at 09:57
  • 1
    session interceptor is used to validate/maintain the session, not for the input values such as `null`. You can manually check if value is `null` or empty. – Aniket Kulkarni Dec 19 '13 at 10:02
  • 1
    @Aniket I solved the problem thanking u for spent your valuable time with me –  Dec 19 '13 at 11:14

1 Answers1

0

Use basicStackHibernate interceptor because to perform List,Edit and Delete operation Hibernate access (Session and/or Transaction).

<interceptor-ref name="defaultStackHibernate" />
Prabha
  • 707
  • 2
  • 11
  • 27