当前位置:网站首页>Getattribute return value is null
Getattribute return value is null
2022-06-24 04:38:00 【Granger_ g】
Problem description
Today, we will develop the verification code verification function , You need to set the mobile phone number and the corresponding verification code to session For later verification , The specific code is as follows :
1. Send the verification code and save it to session in
protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
try {
mresponse = response;
String mobile = req.getParameter("phoneNum");
JSONObject json = null;
// Generate 6 Bit verification code
String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
// Send a text message
sendPhoneNumCode(mobile,verifyCode);
json = new JSONObject();
json.put("mobile", mobile);
json.put("verifyCode", verifyCode);
json.put("createTime", System.currentTimeMillis());
// Store the authentication code in SESSION
HttpSession session = req.getSession();
session.setAttribute("verifyCode", json);
return ;
} catch (Exception e) {
e.printStackTrace();
}
}2. from session Take out the verification code and compare it with the verification code sent by the front end
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);
}The result is the code above session.getAttribute("verifyCode"); The null pointer is abnormal , Say no verifyCode The value of this field .
The reason is to set up verifyCode Field session And take verifyCode Field session Is not a , So I couldn't find it when I took it verifyCode This field .
The solution is to use the settings session selection verifyCode.
So what ?
When obtaining the verification code, say session Save up , Then, when validating, this session Pass on the past ok 了 .
I am here android The specific code implemented on is :
1. obtain session And save
Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie");
String s = cookies.get(0);
System.out.println("-----session:"+s);
// Put this session Save up
AppConfig.session = s;2. use addHeader() Methods will session Pass it to the back end
OkHttpUtils.post()
.url(HttpUrlConfig.URL + HttpUrlConfig.LoginAndRegister)
.addHeader("cookie",AppConfig.session)
.addParams("phoneNum", phoneNum)
.addParams("verifyCode", code)
.build()This problem will be solved . Please comment if you have any questions
边栏推荐
- Go language Chanel memory model
- How to monitor multiple platforms simultaneously when easydss/easygbs platform runs real-time monitoring?
- Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs
- ServiceStack. Source code analysis of redis (connection and connection pool)
- Advanced authentication of uni app [Day12]
- How to install software on ECs is it expensive to rent ECS
- Facebook内部通告:将重新整合即时通讯功能
- Collagenase -- four types of crude collagenase from Worthington
- What is the difference between arm architecture, ARM7, ARM9, STM32, cortex m3 M4, 51 and AVR
- SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义
猜你喜欢

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

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

英特尔 XTU 官方超频工具已支持 Win11 22H2 和 13 代酷睿 Raptor Lake 处理器
Advanced authentication of uni app [Day12]

SAP MTS/ATO/MTO/ETO专题之八:ATO模式2 D+空模式策略用85

Idea创建Servlet 后访问报404问题

mysql - sql执行过程

Doctor application | Hong Kong University of science and Technology (Guangzhou) Mr. Liu Hao recruits the full award doctor / Master in data mining

Weibo International Edition changed its name to Weibo light sharing Edition

What is etcd and its application scenarios
随机推荐
Clang code coverage detection (pile insertion technology)
2020年Android面试题汇总(中级)
英特尔 XTU 官方超频工具已支持 Win11 22H2 和 13 代酷睿 Raptor Lake 处理器
Clickhouse thread occupation troubleshooting and killing
What are the differences between ECs and virtual hosts? Which is better, ECS or VM?
大一下学期期末总结(补充知识漏洞)
图书馆管理后台
Worthington木瓜蛋白酶化学性质和特异性
Idea创建Servlet 后访问报404问题
What if the ECS forgets its password? How can I retrieve my forgotten password?
What is etcd and its application scenarios
What does VPS server mean? What is the difference between a VPS server and an ECS?
Web penetration test - 5. Brute force cracking vulnerability - (5) SMB password cracking
What does IIS mean and what is its function? How does IIS set the size of the web site space on the server?
胶原蛋白酶——Worthington四种类型的粗胶原酶
Web technology sharing | [map] to realize customized track playback
线性回归的损失和优化,机器学习预测房价
华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
TCPIP协议详解
Troubleshoot the high memory consumption of Go program