I doing some project based on authentication using servlet/jsp.When user log in initially using username and password where authentication takes place through login servlet,there I need to save user's email in variable ,say String email
by executing SELECT
query.
I need to access that variable from login servlet to email servlet for sending some sort of OTP to user's email.
How to achieve that using session attribute or any relevant idea??
Asked
Active
Viewed 1,611 times
0

SKJ
- 91
- 1
- 14
-
4Possible duplicate of [How to set session attribute in java?](http://stackoverflow.com/questions/8292940/how-to-set-session-attribute-in-java) – vanje Feb 22 '17 at 20:05
3 Answers
1
Use session.setAttribute()
and session.getAttribute()
methods.
Read javadoc of HttpSession here.
You can refer this complete example.

Sundararaj Govindasamy
- 8,180
- 5
- 44
- 77
1
please use as follows. you can achieve what you need.
<%session.setAttribute( "email", "test@gmail.com" );%>
<%= session.getAttribute( "email" ) %>
The other way that we use.
<c:set var="email" value="test@gmail.com" scope="session"/>
you get this using JS:
var mail ="${email}";

Kumaresan Perumal
- 1,926
- 2
- 29
- 35
1
To save data in session you should use session object from http request like this:
HttpSession session = request.getSession();
session.setAttribute("email", email);
To retrieve data from session object with scriptlet use:
<%= session.getAttribute("email")%>
or
<%= request.getSession().getAttribute("email")%>
You can also use EL expression:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${sessionScope.email}"/>

Joan Bonilla
- 89
- 3