解决Andriod获取Session里面的值为空

问题描述:

安卓课程设计,用户登陆时做了个验证码的功能,在WEB端可以正常运行。

在安卓跑时发现服务器端可以正确获取前端传来的验证码的值,但是从session里取验证码的值时却一直是null。

经过DEBUG发现两者的SESSIONID值不同了,根本不是一个SESSION对象,也不太清楚是什么原因造成的,如下图所示:

解决办法:

曲线救国,服务器端生成并存储验证码后,也存储SESSIONID的值至cookie中并发送给前台。前台表单传递信息时在将该SESSIONID传递过来,后台通过SESSIONID查找指定的SESSION对象,再去获取其里面存储的验证码值进行校验比对。

关键代码如下:

java添加、获取、清除cookie

private final static String URL_CODE = "UTF-8";    

public static void addCookie(HttpServletResponse resp, String key, String value) {
        try {
            Cookie cookie = new Cookie(key, URLEncoder.encode(value, URL_CODE));
            cookie.setPath("/");
            resp.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static String getCookie(HttpServletRequest req, String key) {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            try {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(key)) {
                        return URLDecoder.decode(cookie.getValue(), URL_CODE);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return "-1";
    }

    public static void removeLogin(HttpServletRequest req, HttpServletResponse resp) {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                cookie.setMaxAge(0);
                cookie.setPath("/");
                resp.addCookie(cookie);
            }
        }
    }

服务器端根据SESSIONID获取指定的SESSION对象:参考自https://blog.csdn.net/feicongcong/article/details/76034269

新建一个类MySessionContext.java

/*
 * 采用单例模式设计
 */
public class MySessionContext {
    private static MySessionContext instance;
    private HashMap mymap;

    private MySessionContext() {
        mymap = new HashMap();
    }

    public static MySessionContext getInstance() {
        if (instance == null) {
            instance = new MySessionContext();
        }
        return instance;
    }

    public synchronized void AddSession(HttpSession session) {
        if (session != null) {
            mymap.put(session.getId(), session);
        }
    }

    public synchronized void DelSession(HttpSession session) {
        if (session != null) {
            mymap.remove(session.getId());
        }
    }

    public synchronized HttpSession getSession(String session_id) {
        if (session_id == null)
            return null;
        return (HttpSession) mymap.get(session_id);
    }

}

在新建一个监听类SessionListener.java

public class SessionListener implements HttpSessionListener {
    public static Map userMap = new HashMap();
    private MySessionContext myc = MySessionContext.getInstance();

    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        myc.AddSession(httpSessionEvent.getSession());
    }

    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        myc.DelSession(session);
    }
}

web.xml配置:

    <listener>
        <listener-class>SessionListener</listener-class>
    </listener>

使用方式

   //校验验证码 以sessionID的方式获取指定session对象,在去获取验证码
     MySessionContext myContext= MySessionContext.getInstance();
     HttpSession session = myContext.getSession(sessionID);   //在根据指定的session对象去获取存储其中的校验码的值
     String checkCode = (String) session.getAttribute("CHECKCODE_SERVER");

原文地址:https://www.cnblogs.com/zengcongcong/p/12159125.html

时间: 2024-08-29 14:00:25

解决Andriod获取Session里面的值为空的相关文章

html页面通过ajax请求获取session中的值

在利用springboot进行web开发时,遇到这样一个问题:html如何获取session中的值,实现用户登录系统后首页展示xx欢迎您. 也就是需要实现html通过ajax请求获取session中的值. 1.登录页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <meta charset="utf-8"> <meta http-equiv=&quo

jsp页面中获取session中的值

JSTL标签获取Session: session.setAttribute("age","123"); ${ sessionScope.age} 在页面上显示的就是123了 sessionScope指的是session的范围,类似还有requestScope,pageScope,contextScope 然后后面的age表示的是set属性时的key值 Jsp中获取Session: session是jsp的内置对象,所以你可以直接写在jsp的 <% sessio

一般处理 程序中获取Session中的值?

如果没有引入using System.Web.SessionState;空间 和IRequiresSessionState(或者IReadOnlySessionState)接口, 那么会出现session对象未将实例化的错误. 原文地址:http://blog.51cto.com/11871779/2097125

mybatis中一直获取xml配置文件输入流值为空的类似解决方法

第一个问题:一直在 InputStream stream = StudentDaoImpl.class.getClassLoader().getResourceAsStream("config.xml");返回null,后来发现是resource不能编译到class文件中,在pom中添加 <resources> <!-- 不编译resources下的配置文件 --> <resource> <directory>src/main/resou

JS或jsp获取Session中保存的值

JS是不能读取Session中的值的 . session是服务器对象, javascript是客户端脚本,你能做的操作就是把这个值用 <%=%>输出到页面的javascript中参与运算,而无法直接用js调用的 方法: 你可以做一个AJAX来请求 服务器, 返回session中的值 . 当然你也可以在JSP中使用<%=%>这种东西来获取session中的值,例如: <script language="JavaScript"> var myName=&

session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法

session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法: session_start()放在当前页代码的第一行即可解决该问题. 在本地上session_start()如果不是放在代码的第一行会报错,但还是能正常使用,这时我们可以通过加@屏蔽错误的显示:@session_start().而在sae上,虽然你通过@屏蔽了错误的显示,但仍无法正常使用,获取的session值为空,此时你只要将session_start()放在当前页代码的第一行即可.

WebAPI中无法获取Session对象的解决办法

在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest; base.Init(); } void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) { H

node进阶| 解决表单enctype=&quot;multipart/form-data&quot; 时获取不到Input值的问题

今天在学习node踩到一个坑:form设置enctype="multipart/form-data"上传文件时,无法获取到表单其他input的值. 因为之前上传文件用的是 formidable  方法1:formidable (无法获取其他input的值) 引包 app.js var app = express(); var express = require("express");var router = require("./controller&qu

jsp页面中使用javascript获取后台放在request或session中的值

在JSP页面中.常常使用javascript,可是要出javascript获取存储在request,session, application中的值.例如以下是获取request中的值: 如果后台中有: request.setAttribute("value", "123"); 在前台的javascript中有例如以下获取方式: 1 : var val =  "${value}"; 2: var val = "<%=request.