'how to set timeout for specific attribute in session servlet java jsp?
I know that set session timeout using
<session-config>
<session-timeout>10</session-timeout>
</session-config>
in description it says:
>Session expires if no request within time limit
>
>+ Session inactive
>+ Session id and all attributes destroyed
>+ Request for session attributes returns null
so as description, everything will be removed after specific of time in session if no access. But I want to create id like OTP code and send to user by email so that user can verify and get new password by this way, and I want if after 10 mins there is no access this OTP will be deleted (removed in session)
here is my code that I'm gonna use.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
boolean checkExistMail = checkExistMail(email);
int otp = generateRandomNumber();
request.getSession().setAttribute("otp",otp);
//pseudo code
request.getSession().getAttribute("otp").settimeout(10 minutes);
//end pseudo code
String content = "your otp code is: "+otp;
Mail.sendMail(email,"authentication to get password",content);
response.sendRedirect("authencation.jsp");
}
how can I do this?
note: I'm a student at university, and I 'devised' this method to solve this problem by myself, so maybe this code is kind of wrong approach. If there is anything or any method that is better than this, please let me know.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
