当前位置:网站首页>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
边栏推荐
- Collagenase -- four types of crude collagenase from Worthington
- Openeuler kernel technology sharing issue 20 - execution entity creation and switching
- Abnova膜蛋白脂蛋白体解决方案
- Wide & deep model and optimizer understand code practice
- Web technology sharing | [map] to realize customized track playback
- An interface testing software that supports offline document sharing in the Intranet
- TCPIP协议详解
- Have you learned all these routines to solve bugs?
- Through the fog: location notes of Flink crash with a multi component timeout
- Opengauss version 3.0 source code compilation and installation guide
猜你喜欢

SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义

External network access SVN server (external network access SVN server deployed on the cloud)

重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家

Introduction to C language custom types (structure, enumeration, union, bit segment)

apipost接口断言详解

什么是数据中台

微博国际版更名为微博轻享版

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

TCPIP协议详解

『应急响应实践』LogParser日志分析实践
随机推荐
DP summary of ACM in recent two weeks
[receive] new benefits of 60 yuan / year? Lowest in history! Double 11 has now begun to seize resources! Get started quickly!!
Worthington脱氧核糖核酸酶I特异性和相关研究
Cadence OrCAD Capture 批量修改网络名称的两种最实用的方法图文教程及视频演示
Summary of Android interview questions in 2020 (intermediate)
How does ECS select bandwidth? What types of servers do you usually have?
Easyanticheat uses to inject unsigned code into a protected process (1)
大一下学期期末总结(补充知识漏洞)
How to enlarge the ECS page? How to select ECS instance specifications?
How to add a domain name to ECS? What are the advantages of ECS?
How does the compiler put the first instruction executed by the chip at the start address of the chip?
How to build a website for ECS is the price of ECS very expensive
What does IIS mean and what is its function? How does IIS set the size of the web site space on the server?
What is Ping? How can the server disable Ping?
近两周ACM之DP总结
What is the data center
How to install software on ECs is it expensive to rent ECS
What are the advantages of ECS? Is ECS better than VM?
编译器是如何将芯片执行的第一个指令放到芯片起始地址的?
微博国际版更名为微博轻享版