Struts2 - retrieve session object using another session object as key - java

I'm new to struts2 and I'm trying to retrieve a session object using a dynamic session key.
My app flow is like this: User will hit the action from their browser
http://localhost:8080/prjcr/pad.action?userid=p02585#test.org
In the action class, I retrieve a list of values from a web service using p02585#test.org request param and store them in the session as follows:
//get the request parameter
userid=ServletActionContext.getRequest().getParameter("userid);
QDefn[] qDefn = proxy.getQDefns(userid); //webservice call
List<QDefn> qdList = new ArrayList<QDefn>();
qdList.add(Arrays.asList(qDefn));
Extract userid part of the request parameter to be used as the session key
userid = userid.substring("UserId", userid.substring(0, userid.indexof('#'));
//The key itself is what we receive in the request parameter
ActionContext.getContext().getSession().put("UserId", userid);
and then push the associated list of values into session
ActionContext.getContext().getSession().put(userid, qdList);
and forward to a JSP that displays this list in a select drop down as follows:
<s:select name="qdefn"
id="qdefn"
list="#session.%{#session.UserId}" ---What is the notation here??
listKey="defnName"
listValue="defnName"
headerKey="ALLQD"
headerValue="All" > </s:select>
I've tried to pull the qdList from the session in jsp using a dynamic session key which is the userid.
In java, we do it as session.get(userid). I am not able to get in terms with the OGNL notation yet. So, I dont know how to do it in Struts2/OGNL.
Thanks in advance

if you do a
System.out.println(ActionContext.getContext().getSession().getClass());
in your action you will get 'class org.apache.struts2.dispatcher.SessionMap', but if you do a
out.println(session.getClass());
in your jsp you will get 'class org.apache.catalina.session.StandardSessionFacade' or something like that (i use tomcat)
so try
session.getAttribute(userid);
instead of
session.get(userid);

Related

retrieve an array of from vuejs and add it to session variable

I'm stuck with a problem:
i have an array of object in vuejs
TablePlayers[]
this.TablePlayers.push({'message': this.returnmsg, 'player': true, 'Time': time});
i send it to my java controller with:
axios.get("http://localhost:8080/SetTablePlayers/"+this.TablePlayers)
then i add this table of object to java session with :
HttpSession session = request.getSession();
session.setAttribute("TablePlayers", TablePlayers);
3.and when i would get this object from session :
TablePlayers= (List<Players>)session.getAttribute("TablePlayers");
it returns null.
how can i fix that thanks, and this is right?
You cannot send your JSON data that way. Please use an http POST method and send the data into the body of the request.
axios.post("http://localhost:8080/SetTablePlayers/", this.TablePlayers)
of course you have to adapt also your server code.
GET method should be used to obtain something from the server instead.

Putting attribute back into a request in java

I'm having trouble putting an attribute back into a request in java.
Basically I have a jsp that gets an 'id' from the link to that page:
Integer prodId = Integer.parseInt(request.getParameter("productId"));
Then in my command (I'm using the command pattern) I get the 'id' from the request and then return to the same jsp page but then the 'id' is no longer in the request and I get a number format exception
I have tried:
request.setAttribute("prodId", id);
but to no avail
Anyone got any ideas?
Thanks.
Request Attribute and Request Parameters are different things.
1.Try retrieving it as :
request.getAttribute("prodId");
from your JSP, once you set the request attribute in command class, as
request.setAttribute("prodId", id);
This code gets the request parameter, not request attribute.
request.getParameter("productId");
2.Or, if you are forwarding to the JSP page from the Command class :
//this will let you use request.getParameter() in JSP.
request.getRequestDispatcher(jspFilePath).forward(request,response);

How do I retrieve the jsessionid from URL in JSP?

I know how to pass the jsessionid to the URL. I encode the url, which will look like this:
mysite.com;jsessionid=0123456789ABCDEF (http)
Does there exist a built-in method to retrieve the jsessionid from the URL, using Java? I found this method in the javadocs, isRequestedSessionIdFromURL, but it doesn't help me actually retrieve the value. Do I have to build my own retrieval method?
Thank you.
JSP has an implicit session object, similar the request object. It is an instance of java.servlet.http.HttpSession, which has the method getId().
So, you should be able to just do session.getId() in your JSP page.
Cookieless sessions are achieved in Java by appending a string of the format ;jsessionid=SESSION_IDENTIFIER to the end of a URL. To do this, all links emitted by your website need to be passed through either HttpServletRequest.encodeURL(), either directly or through mechanisms such as the JSTL tag. Failure to do this for even a single link can result in your users losing their session forever.
session.getId() should be able to get you the session id. WLS would be doing the actual parsing of the URl to retrieve the session id once it identifies that the session id is not stored in the cookie or in hidden form fields. The sequence usually is Cookie - URL - Hidden Form Fields.

Saving data inside user session getting shared between user

i am having one jsp file with the following code
String name=request.getParameter("user");
if(name==null)
name=(String)request.getSession().getAttribute("name");
else
request.getSession().setAttribute("name", name);
i assume if the page get any request with user as parameter, it will save the value to that particular user session and in case the get request is not having any 'user' parameter it will try to read the user value from the session. The code is working perfectly when i host it from my local server(glassfish).
But when i upload it into some remote host, things are getting weird.
When i hit the page with parameter 'user', its saving the value in the session. But then again if i hit the page from some other browser(or after clearing cookie), its retrieving the previous value saved, instead of returning null
Am i doing something wrong, actually im pretty new to Java EE.
You cannot get previous value saved unless
1) Your session is not finished and same session is getting extended. Can you explain how you are clearing the cookies.
2) Are you typing the URL or trying to refresh the page after deleting the cookies.

Get session by id in ajax call

Is there a way to have access to session in a AJAX call made to a JAVA server.
On the server, the request object has both the session and cookies properties NULL.
I can pass though the session id as a parameter, but how can I access the session by ID?
Edit
Using session.getSession(false); returns null, while session.getSession(true); obviously returns a new session, with another id.
The best way to deal with this is to append ";jsessionid=" at the end of the url. For instance, in a jsp:
<script type="text/javascript">
...
xhr.open("GET", url + ";jsessionid=<%=pageContext.getSession().getId()%>");
...
</script>
The line:
xhr.open("GET", url + ";jsessionid=<%=pageContext.getSession().getId()%>");
is rendered as:
xhr.open("GET", url + ";jsessionid=6EBA4F94838796DC6D653DCA1DD06373");
It sounds like you don't have a session!
Make sure when the load containing the AJAX script, the session is created on the server.
session.getSession(true);
If this is stored as a cookie, then your AJAX call will submit it back to the server when it fires.
In order to access the session you do not need the session id. This is all done for you behind the scenes by your servlet container. To get the session for a particluar request all you need to do is:
HttpSession session = request.getSession(false);
where request is your HttpServletRequest. The false arg means "do not create session if it does not exist". Of course use "true" if you want the session to be created if it doesn't exist.

Categories

Resources