0

In my web services class using(jersey) @Context I injected HttpServletRequest, HttpServletResponse, ServletContext objects for authentication i called a ValidateUser servlet.

@Path("/server")
public class WebServer { 
    @Context 
    private ServletContext context; 

    @Context
    private HttpServletRequest request;

    @Context
    private HttpServletResponse response;

    @POST
    @Path("/auth")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createTrackInJSON(Track track) throws IOException, ServletException{ 
    ....
    context.getRequestDispatcher("/ValidateUser")include(request, response);
    ...
    }        
}

in ValidateUser.java i created session after validations as:

HttpSession session = request.getSession(true);

Up to now everything is working fine but when I call:

HttpSession session = request.getSession(false);
session.invalidate();

in Logout Servlet session is null. I know restful web services are stateless but i need to maintain session at server side is there any way i can achieve this? What about @PerSession annotation? Will it be useful in this scenario?

Gray
  • 115,027
  • 24
  • 293
  • 354
P Srinivas Goud
  • 886
  • 1
  • 12
  • 30

1 Answers1

0

Here are some ways to debug your sessions:

  • Can you see the session cookie being created on your client?
  • Are you sure you aren't using multiple frontends? Maybe you need to persist your sessions to the database?
  • Could the session's have timed out? Could it be a long time between the validate call and the logout?

What about @PerSession annotation? Will it be useful in this scenario?

The @PerSession is going to use the same underlying session information. I don't think it is going to solve your problem.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • thanks for reply, I came to know that its difficult to maintain session in restful web services so i implemented token mechanism.I store token in database instead of creating session and for every request i am checking the token in db, in logout i am deleting token from db. – P Srinivas Goud Mar 26 '13 at 05:57
  • Many web frameworks allow you to persist your session in the database which is similar to the mechanism you are using. Glad it's working for your @PSrinivasgoud. – Gray Mar 26 '13 at 12:03