当前位置:网站首页>getAttribute 返回值为null
getAttribute 返回值为null
2022-06-24 03:55:00 【Granger_g】
问题描述
今天开发验证码验证功能,需要将手机号和对应的验证码设置到session中以便后面的验证,具体代码如下:
1.发送验证码并把验证码保存到session中
protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
try {
mresponse = response;
String mobile = req.getParameter("phoneNum");
JSONObject json = null;
//生成6位验证码
String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
//发送短信
sendPhoneNumCode(mobile,verifyCode);
json = new JSONObject();
json.put("mobile", mobile);
json.put("verifyCode", verifyCode);
json.put("createTime", System.currentTimeMillis());
// 将认证码存入SESSION
HttpSession session = req.getSession();
session.setAttribute("verifyCode", json);
return ;
} catch (Exception e) {
e.printStackTrace();
}
}2.从session中取出验证码和前端发送过来的验证码进行比对
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mobile = request.getParameter("phoneNum");
String verifyCode = request.getParameter("verifyCode");
HttpSession session = request.getSession();
JSONObject json = (JSONObject) session.getAttribute("verifyCode");
System.out.println("request_phoneNum:"+mobile);
System.out.println("request_verifyCode:"+verifyCode);
String json_phoneNum = (String) json.get("mobile");
String json_verifyCode = (String) json.get("verifyCode");
System.out.println("json_phoneNum:"+json_phoneNum);
System.out.println("json_verifyCode:"+json_verifyCode);
}结果上面的代码 session.getAttribute("verifyCode");报空指针异常,说取不到verifyCode这个字段的值。
原因是设置verifyCode字段时的session和取verifyCode字段时的session不是一个,所以在取的时候就找不到verifyCode这个字段了。
解决办法是用设置时session去取verifyCode。
那怎么搞呢?
在获取验证码时讲session保存起来,然后验证时将这个session再传递过去就ok了。
我在android上实现的具体代码是:
1.获取session并保存
Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie");
String s = cookies.get(0);
System.out.println("-----session:"+s);
//将这个session保存起来
AppConfig.session = s;2.用addHeader()方法将session传递给后端
OkHttpUtils.post()
.url(HttpUrlConfig.URL + HttpUrlConfig.LoginAndRegister)
.addHeader("cookie",AppConfig.session)
.addParams("phoneNum", phoneNum)
.addParams("verifyCode", code)
.build()到此问题就解决了。有问题欢迎评论
边栏推荐
- Web technology sharing | [map] to realize customized track playback
- Ribbon
- The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects
- openEuler Kernel 技术分享第 20 期 | 执行实体创建与切换
- Easygbs video playback protocol only webrtc can play. Troubleshooting
- Mac CentOS installation phpredis
- 多任务视频推荐方案,百度工程师实战经验分享
- The collection method of penetration test, and which methods can be used to find the real IP
- 抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
- 华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
猜你喜欢

Abnova膜蛋白脂蛋白体解决方案

Clang代码覆盖率检测(插桩技术)

由浅入深的混合精度训练教程

什么是数据中台

C语言自定义类型的介绍(结构体,枚举,联合体,位段)
Summary of Android interview questions in 2020 (intermediate)

一款支持内网脱机分享文档的接口测试软件

How does the compiler put the first instruction executed by the chip at the start address of the chip?

Multi task video recommendation scheme, baidu engineers' actual combat experience sharing

apipost接口断言详解
随机推荐
Easyplayer consumes traffic but does not play video and reports an error libdecoder Wasm404 troubleshooting
I have an agreement with IOT
How to monitor the operation of easygbs service in real time?
英特尔 XTU 官方超频工具已支持 Win11 22H2 和 13 代酷睿 Raptor Lake 处理器
[2021 "shadow seeking" medical artificial intelligence algorithm competition] frequently asked questions related to Ti-One products
Cloud development CMS Enterprise Edition demand survey
ServiceStack. Source code analysis of redis (connection and connection pool)
Web penetration test - 5. Brute force cracking vulnerability - (6) VNC password cracking
mysql中表的命名
Mac CentOS installation phpredis
华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
一款支持内网脱机分享文档的接口测试软件
How to set the domain name on the server what is the role of the domain name
一文简述:供应链攻击知多少
由浅入深的混合精度训练教程
MySQL cases SQL causes 100% CPU utilization
Openeuler kernel technology sharing issue 20 - execution entity creation and switching
2. in depth tidb: entry code analysis and debugging tidb
Multi task video recommendation scheme, baidu engineers' actual combat experience sharing
Gpt/gpt2/dialogpt detailed explanation comparison and application - text generation and dialogue