How to keep session.attribute from jsp to servlet
I have a Login Servlet where i set the attribute user logged:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
... OMITTED
request.getSession().setAttribute("user", user_name);
request.getRequestDispatcher("index.jsp").forward(request, response);
... OMITTED
}
From the index.jsp I go to my quit.jsp, until here i can access to the user logged through getAttribute()
. But in my quit.jsp, I ask the user logged just for his password and call the Quit Servlet. The problem is that in the Quit Servlet when I try to access to the current user logged with getAttribute()
, the attribute "user" is null. So my question is: how can I use a persistent attribute created in a servlet which will be used in jsp and servlets?
java jsp servlets
add a comment |
I have a Login Servlet where i set the attribute user logged:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
... OMITTED
request.getSession().setAttribute("user", user_name);
request.getRequestDispatcher("index.jsp").forward(request, response);
... OMITTED
}
From the index.jsp I go to my quit.jsp, until here i can access to the user logged through getAttribute()
. But in my quit.jsp, I ask the user logged just for his password and call the Quit Servlet. The problem is that in the Quit Servlet when I try to access to the current user logged with getAttribute()
, the attribute "user" is null. So my question is: how can I use a persistent attribute created in a servlet which will be used in jsp and servlets?
java jsp servlets
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54
add a comment |
I have a Login Servlet where i set the attribute user logged:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
... OMITTED
request.getSession().setAttribute("user", user_name);
request.getRequestDispatcher("index.jsp").forward(request, response);
... OMITTED
}
From the index.jsp I go to my quit.jsp, until here i can access to the user logged through getAttribute()
. But in my quit.jsp, I ask the user logged just for his password and call the Quit Servlet. The problem is that in the Quit Servlet when I try to access to the current user logged with getAttribute()
, the attribute "user" is null. So my question is: how can I use a persistent attribute created in a servlet which will be used in jsp and servlets?
java jsp servlets
I have a Login Servlet where i set the attribute user logged:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
... OMITTED
request.getSession().setAttribute("user", user_name);
request.getRequestDispatcher("index.jsp").forward(request, response);
... OMITTED
}
From the index.jsp I go to my quit.jsp, until here i can access to the user logged through getAttribute()
. But in my quit.jsp, I ask the user logged just for his password and call the Quit Servlet. The problem is that in the Quit Servlet when I try to access to the current user logged with getAttribute()
, the attribute "user" is null. So my question is: how can I use a persistent attribute created in a servlet which will be used in jsp and servlets?
java jsp servlets
java jsp servlets
asked Nov 20 '18 at 13:50
GuillermoGuillermo
233
233
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54
add a comment |
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54
add a comment |
2 Answers
2
active
oldest
votes
Your problem might not be so easy to solve. I have had a similar situation so I can give you some investigation pointers:
The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement.
As a short cut:
Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false);
and this as well which might solve your problem:
request.getSession().setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered.
a logout would call:
session.removeAttribute("attribute");
session.invalidate();
Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). One is Login the other is Quit, they both respond to POST/GET. I suppose
they have two different implementations and I think that they do not share the same session object. So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval.
add a comment |
In a JSP
file, the HttpServletRequest
is available with variable name request
by default. You can use it same way in the JSP
file too.
So
request.getSession().setAttribute("user", user_name);
will be available in JSP file also.
<%
request.getSession().setAttribute("user", user_name);
%>
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394496%2fhow-to-keep-session-attribute-from-jsp-to-servlet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your problem might not be so easy to solve. I have had a similar situation so I can give you some investigation pointers:
The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement.
As a short cut:
Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false);
and this as well which might solve your problem:
request.getSession().setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered.
a logout would call:
session.removeAttribute("attribute");
session.invalidate();
Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). One is Login the other is Quit, they both respond to POST/GET. I suppose
they have two different implementations and I think that they do not share the same session object. So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval.
add a comment |
Your problem might not be so easy to solve. I have had a similar situation so I can give you some investigation pointers:
The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement.
As a short cut:
Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false);
and this as well which might solve your problem:
request.getSession().setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered.
a logout would call:
session.removeAttribute("attribute");
session.invalidate();
Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). One is Login the other is Quit, they both respond to POST/GET. I suppose
they have two different implementations and I think that they do not share the same session object. So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval.
add a comment |
Your problem might not be so easy to solve. I have had a similar situation so I can give you some investigation pointers:
The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement.
As a short cut:
Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false);
and this as well which might solve your problem:
request.getSession().setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered.
a logout would call:
session.removeAttribute("attribute");
session.invalidate();
Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). One is Login the other is Quit, they both respond to POST/GET. I suppose
they have two different implementations and I think that they do not share the same session object. So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval.
Your problem might not be so easy to solve. I have had a similar situation so I can give you some investigation pointers:
The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement.
As a short cut:
Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false);
and this as well which might solve your problem:
request.getSession().setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered.
a logout would call:
session.removeAttribute("attribute");
session.invalidate();
Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). One is Login the other is Quit, they both respond to POST/GET. I suppose
they have two different implementations and I think that they do not share the same session object. So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval.
edited Nov 20 '18 at 20:32
answered Nov 20 '18 at 18:18
ApolloApollo
362
362
add a comment |
add a comment |
In a JSP
file, the HttpServletRequest
is available with variable name request
by default. You can use it same way in the JSP
file too.
So
request.getSession().setAttribute("user", user_name);
will be available in JSP file also.
<%
request.getSession().setAttribute("user", user_name);
%>
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
add a comment |
In a JSP
file, the HttpServletRequest
is available with variable name request
by default. You can use it same way in the JSP
file too.
So
request.getSession().setAttribute("user", user_name);
will be available in JSP file also.
<%
request.getSession().setAttribute("user", user_name);
%>
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
add a comment |
In a JSP
file, the HttpServletRequest
is available with variable name request
by default. You can use it same way in the JSP
file too.
So
request.getSession().setAttribute("user", user_name);
will be available in JSP file also.
<%
request.getSession().setAttribute("user", user_name);
%>
In a JSP
file, the HttpServletRequest
is available with variable name request
by default. You can use it same way in the JSP
file too.
So
request.getSession().setAttribute("user", user_name);
will be available in JSP file also.
<%
request.getSession().setAttribute("user", user_name);
%>
answered Nov 20 '18 at 16:58
theapache64theapache64
2,78322147
2,78322147
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
add a comment |
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
Yes but I want to use that attribute in another servlet and in the JSP.
– Guillermo
Nov 21 '18 at 16:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394496%2fhow-to-keep-session-attribute-from-jsp-to-servlet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you have a problem in a Servlet but choose not to post the code of this Servlet????
– Alan Hay
Nov 20 '18 at 14:54