Java Soap Glassfish在登录后获得会话

时间:2013-11-29 07:24:52

标签: java session authentication soap glassfish

我有网络方法登录

    @RolesAllowed("admin")
    @Override
    public String LogIn(String UserName, String Password, String PhoneNumber) {
            String username = "";
            String password = "";
        if (UserName != null) {
            username = UserName;
        }
        if (Password != null) {
            password = Password;
        }
        System.out.println("username is"+username);
        System.out.println("password is"+password);

        if (username.equals("admin") && password.equals("admin")) {
            String session = session_id.AddObject(PhoneNumber);
            return session;

        } else {
            return null;
        }

    }

我发现,设置Session可能是,扩展httpServlet的类,如下所示:

 @RolesAllowed("admin")
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)  
            throws ServletException, IOException {  


            HttpSession session = req.getSession(true);  
            session.setAttribute("doneSessed", nameInput);  
            //HttpSession httpSession = request.getSession();
    } 

设置Session客户端之后,我想要保存输入参数来将此参数与session_id进行比较(我想用于此SqLite)。

问题,成功登录后如何设置会话?

1 个答案:

答案 0 :(得分:1)

您可以添加:

   @Resource
    WebServiceContext webServiceContext;

    public String LogIn(String UserName, String Password, String PhoneNumber) {
     ...
        MessageContext mc = webServiceContext.getMessageContext();
        HttpSession session =    ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        if (session == null)
             throw new WebServiceException("No Session found");
     ...