当前位置:网站首页>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

原网站

版权声明
本文为[Granger_ g]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206232358125267.html