当前位置:网站首页>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()到此问题就解决了。有问题欢迎评论
边栏推荐
- What is the data center
- Worthington弹性蛋白酶的应用和相关研究
- web渗透测试----5、暴力破解漏洞--(6)VNC密码破解
- Easygbs video playback protocol only webrtc can play. Troubleshooting
- 大一下学期期末总结(补充知识漏洞)
- Gpt/gpt2/dialogpt detailed explanation comparison and application - text generation and dialogue
- Clang code coverage detection (pile insertion technology)
- web渗透测试----5、暴力破解漏洞--(8)PostgreSQL密码破解
- How to do the right thing in digital marketing of consumer goods enterprises?
- How to adjust the alarm information that remains unchanged after paging is selected on the easygbs alarm page?
猜你喜欢

博士申请 | 香港科技大学(广州)刘浩老师招收数据挖掘方向全奖博士/硕士

15+ urban road element segmentation application, this segmentation model is enough

How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!
Advanced authentication of uni app [Day12]
Summary of Android interview questions in 2020 (intermediate)

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

apipost接口断言详解

Abnova多肽设计和合成解决方案

Weibo International Edition changed its name to Weibo light sharing Edition

共建欧拉社区 共享欧拉生态|携手麒麟软件 共创数智未来
随机推荐
DP summary of ACM in recent two weeks
How does the compiler put the first instruction executed by the chip at the start address of the chip?
Openeuler kernel technology sharing issue 20 - execution entity creation and switching
Next. JS + cloud development webify creates an excellent website
Training course of mixed accuracy from simple to deep
IDC, Youshang cloud data on cloud (COS) best practices
web渗透测试----5、暴力破解漏洞--(6)VNC密码破解
Analysis of grafana SSO authentication process based on keyloak
How to spell the iframe address of the video channel in easycvr?
Demonstration of C language structure function research
How to monitor the operation of easygbs service in real time?
3. go deep into tidb: perform optimization explanation
Maintain the visibility of data automation: logging, auditing and error handling of the bridge of knowledge and action
抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
Worthington弹性蛋白酶的应用和相关研究
web渗透测试----5、暴力破解漏洞--(5)SMB密码破解
事件
应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构
"." in the structure of C language And "- & gt;" Differences between
mysql - sql执行过程