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.
Related
I was given this as an in class problem and recievd 0 as the result, but I can't find the fault with it. The questions is Write the lines of code you would need to use in a servlet to retrieve a parameter
from an incoming request and add it to the session as an attribute:
My answer:
String param1= request.getParameter("param1");
HttpSession session= request.getSession();
String parameter1= (String)request.getAttribute("param1");
session.setAttribute("param1", parameter1);
Also, is the '(String)' necessary in the second line? or just good practice?
Thanks :)
You've made a very common mistake, confusing attributes with parameters. In your code you have:
String parameter1= (String)request.getAttribute("param1");
Attributes can be thought of as meta-data of the request. For example, if the request is made via SSL then you can get data about the request from the attributes. Take a look at the documentation for getAttribute for more details.
Parameters, on the other hand, can be used to get URL parameters. Your last question actually points you in the correct direction - getParameter() already returns a String so you don't need to cast it.
Properly written the line above should be:
String parameter1= request.getParameter("param1");
getParameter() - used to get url parameter from request in server side(java side).
Where as If you want to fetch any values from java side to jsp(view side)
you can use setAttribute() in Server side(Java side) and get the value using getAttribute() from 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.
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);
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.
I understand that if we use the following statement
HttpSession session = request.getSession();
Will create the Unique session id, Create Cookie and associate Cookie with the Session id.
and helps the container to keep track and identify the clients.
Yes, My question, is there a possibility for me to see the cookie header and Unique Id created by this statement request.getSession()?
You can retrieve a HTTP Header using
HttpServletRequest.getHeader.
Although a session can be created by calling HttpServletRequest.getSession(true)
it's rather done by the webcontainer. As edl already wrote HttpServletRequest.getSession().getId() returns the session id.
You can see it using any HTTP header tracker tool. Firebug for example shows the headers in the Net panel. Here's a screenshot (click here for full size):
Any newly created cookie will appear as Set-Cookie header in the response. The client will send the same value back as Cookie header in the subsequent requests in the same session so that the server can identify the client session. For a JSP/Servlet webapplication, your interest is the cookie with the name JSESSIONID.
You can use session.getId() for the ID I believe. Not sure about the header.
I found more information in the following URL
http://www.javacertifications.net/javacert/session.jsp